zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS swift extensions

ios swift Extensions
2023-09-14 09:04:15 时间

返回上级目录:swift,oc语法(苹果文档)和对比

1.苹果官方文档

Extensions - The Swift Programming Language

2.示例(亲测)

2.1 不能添加存储属性

在这里插入图片描述

2.2 本类和扩展间相互调用属性和方法

在这里插入图片描述

3.示例代码

//
//  ViewController.swift
//  XYBluetoothCentralManager
//
//  Created by macvivi on 2020/10/29.
//  Copyright © 2020 macvivi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var test: String = ""
    func testFunc() {}
    
    @IBOutlet var tableView: UITableView!
    
    @IBAction func scanClick(_ sender: Any) {
    }
    
    @IBAction func stopClick(_ sender: Any) {
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        
        testFunc2()
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let str = test
        testFunc()
        testFunc2()
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}

extension ViewController: UITableViewDelegate {
    func testFunc2() {}
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}