2017-06-13 163 views
0

我無法將Swift 2.1中的代碼翻譯成Swift 3.0。我在塊「lazy var」(第3行和第4行)上發現錯誤。錯誤消息:「實例成員'çontentView'不能用於類型'MyView'」。遷移Swift 2.1到Swift 3.0

但在Swift 2.1中的代碼工作。

有人可以幫助我嗎?

我的代碼:

@IBDesignable 類MyView的:UIView的{

private lazy var __once:() = {() -> Void in 
    let bundle = Bundle(for: type(of: self)) 

    MyView.contentView = UINib(nibName: "MyView", bundle: bundle).instantiate(withOwner: self, options: nil)[0] as! UIView 
    MyView.contentView.translatesAutoresizingMaskIntoConstraints = false 

    self.addSubview(MyView.contentView) 

    let view = ["contentView": MyView.contentView] 
    self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[contentView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: view)) 
    self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[contentView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: view)) 

    MyView.titleMyLabel.text = MyView.myTitle 
    MyView.descriptionMyLabel.text = MyView.myDescription 
    MyView.mobileNumberLabel.text = MyView.numberMobile 
    MyView.myBarBotton.botaoMudou(.hight) 
    MyView.myBarBotton.botaoMudou(.left) 
    MyView.myBarBotton.botaoMudou(.center) 

    self.setNeedsUpdateConstraints() 
}() 

@IBInspectable var myTitle: String? { 
    didSet { 
     titleMyLabel.text = myTitle 
    } 
} 
@IBInspectable var myDescription: String? { 
    didSet { 
     descriptionMyLabel.text = myDescription 
    } 
} 
@IBInspectable var numberMobile: String? { 
    didSet { 
     mobileNumberLabel.text = numberMobile 
    } 
} 

@IBInspectable var rawStyleLeftButton: Int { 
    set(newValue) { 
     myBarBotton.rawstyleLeftButton = newValue 
    } 
    get { 
     return myBarBotton.rawstyleLeftButton 
    } 
} 
@IBInspectable var rawStyleHightButton: Int { 
    set(newValue) { 
     myBarBotton.rawstyleHightButton = newValue 
    } 
    get { 
     return myBarBotton.rawstyleHightButton 
    } 
} 
@IBInspectable var rawStyleCenterButton: Int { 
    set(newValue) { 
     myBarBotton.rawstyleCenterButton = newValue 
    } 
    get { 
     return myBarBotton.rawstyleCenterButton 
    } 
} 

@IBInspectable var titleLeftButtom: String? { 
    set(newValue) { 
     myBarBotton.myTitleBotaoEsquerdo = newValue 
    } 
    get { 
     return myBarBotton.myTitleBotaoEsquerdo 
    } 
} 
@IBInspectable var titleCenterButtom: String? { 
    set(newValue) { 
     myBarBotton.myTitleBotaoCentral = newValue 
    } 
    get { 
     return myBarBotton.myTitleBotaoCentral 
    } 
} 
@IBInspectable var titleHightButton: String? { 
    set(newValue) { 
     myBarBotton.myTitleBotaoDireito = newValue 
    } 
    get { 
     return myBarBotton.myTitleBotaoDireito 
    } 
} 

var styleLeftButton: styleMyBarBotton { 
    set(newValue) { 
     myBarBotton.styleLeftButton = newValue 
    } 
    get { 
     return myBarBotton.styleLeftButton 
    } 
} 
var styleCenterButton: styleMyBarBotton { 
    set(newValue) { 
     myBarBotton.styleCenterButton = newValue 
    } 
    get { 
     return myBarBotton.styleCenterButton 
    } 
} 
var styleHightButton: styleMyBarBotton { 
    set(newValue) { 
     myBarBotton.styleHightButton = newValue 
    } 
    get { 
     return myBarBotton.styleHightButton 
    } 
} 

fileprivate var token: Int = 0 
fileprivate var contentView: UIView! 

@IBOutlet weak var myBarBotton: myBarBottonView! 
@IBOutlet fileprivate weak var titleMyLabel: UILabel! 
@IBOutlet fileprivate weak var descriptionMyLabel: UILabel! 
@IBOutlet fileprivate weak var mobileNumberLabel: UILabel! 


fileprivate func initialization() { 
    _ = self.__once 
} 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    initialization() 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    initialization() 
} 

}

回答

0

在有錯誤的每一行改變MyView的自我,用下面的代碼後,你的類編譯在操場上沒有錯誤。

private lazy var __once:() = {() -> Void in [weak self] 
     guard let strongSelf = self else{return} 
     let bundle = Bundle(for: type(of: strongSelf)) 

     strongSelf.contentView = UINib(nibName: "MyView", bundle: bundle).instantiate(withOwner: self, options: nil)[0] as! UIView 
     strongSelf.contentView.translatesAutoresizingMaskIntoConstraints = false 

     strongSelf.addSubview(strongSelf.contentView) 

     let view = ["contentView": strongSelf.contentView] 
     strongSelf.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[contentView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: view)) 
     strongSelf.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[contentView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: view)) 

     strongSelf.titleMyLabel.text = MyView.myTitle 
     strongSelf.descriptionMyLabel.text = MyView.myDescription 
     strongSelf.mobileNumberLabel.text = MyView.numberMobile 
     MyView.myBarBotton.botaoMudou(.hight) 
     MyView.myBarBotton.botaoMudou(.left) 
     MyView.myBarBotton.botaoMudou(.center) 

     strongSelf.setNeedsUpdateConstraints() 
    }()