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) {
}
}