2017-06-13 14 views
0

スウィフト2.1のコードをスウィフト3.0に翻訳するのに問題があります。ブロック "lazy var"(3行目と4行目)にエラーが見つかりました。エラーメッセージ: "インスタンスメンバー 'çontentView'をタイプ 'MyView'で使用できません。マイグレーションスウィフト2.1からスウィフト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() 
    }() 
関連する問題