Swift 扩展

扩展里面能写什么

//MARK: -  扩展中都可以写什么东西
extension ViewController {

    //扩展中可以写方法
    func needSpeed() {
        print("金坷垃")
    }

    //扩展中可以写计算型的属性  不能写存储型的属性
    var subTitle: String {
        set (newTitle){
            print(newTitle)
        }
        get{
            return "金坷垃"
        }
    }

    //扩展中可以扩展新的构造函数 只能是`便利`构造函数
    convenience init(sunTitle: String) {
    
        //最后调用指定的构造函数
        self.init(nibName: nil, bundle: nil)
    }


   /// 这个枚举就是个嵌套类型, 也可以直接写在 类/结构体 里面,不写在扩展里
    enum `Type`: Int { //设置一个原始值, Int 类型
        case normal //这个默认就是0
        case light  //默认1
        case dark   //默认2
    }


}

你可能感兴趣的:(Swift 扩展)