便利构造函数

2018-01-25  本文已影响0人  xiaoke123456

     convenience:便利,使用convenience修饰的构造函数叫做便利构造函数  

     便利构造函数通常用在对系统的类进行构造函数的扩充时使用。  

     便利构造函数的特点:  

     1、便利构造函数通常都是写在extension里面  

     2、便利函数init前面需要加载convenience   

     3、在便利构造函数中需要明确的调用self.init()  

栗子:对UITextField进行扩展

import UIKit

extension UITextField{

    convenienceinit(frame:CGRect,placeholderStr:String,color:UIColor,font:CGFloat=14) {

        self.init(frame:frame)

        self.placeholder= placeholderStr

        self.textColor= color

        self.font=UIFont.systemFont(ofSize:font)

        self.borderStyle = UITextBorderStyle.roundedRect

}

}

上一篇 下一篇

猜你喜欢

热点阅读