简体   繁体   English

升级到Swift 2.0会导致NSCODER错误

[英]Upgrading to Swift 2.0 causes an error with NSCODER

I just updated my xcode 6.4 to 7 and converted my old Swift syntax to the latest one. 我刚刚将xcode 6.4更新为7,并将旧的Swift语法转换为最新的。 Since then, I, obviously, have been having a lot of errors and could not solve the following error. 从那时起,很明显,自那时以来,我一直遇到很多错误,无法解决以下错误。 I screenshotted it. 我截图了。 在此处输入图片说明

Does anyone know how to tackle this error issue? 有谁知道如何解决这个错误问题?

Thank you 谢谢

The solution: 解决方案:

  1. Remove the extra ? 删除多余的? on line 15. 在第15行。
  2. Add public to your init on line 25. 在第25行将public添加到您的init

The explanation: 说明:

Your required init must be public because the class is public. 因为类是公共的,所以您required init必须是公共的。 By not specifying visibility, it defaults to internal. 通过不指定可见性,它默认为内部。 Line 25 should read: 第25行应显示为:

public required init?(coder aDecoder: NSCoder) {

The reason DShaw's solution worked for him but not for you is because his class had internal visibility while yours is public. DShaw的解决方案对他有用但对您不起作用的原因是,当您公开时,他的班级具有内部可见性。

Just delete the first required Init. 只需删除第一个必需的Init。

The convert process from swift 1.2/1 to 2 in my projects required me to do so as well. 在我的项目中,从1.2 / 1迅速转换为2的转换过程也需要我这样做。

This works fine for me in Xcode 7: 这在Xcode 7中对我来说很好用:

import UIKit

class TextView: UITextView {

internal var placeholderText: String = "Tap to edit"

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

required override init(frame: CGRect, textContainer: NSTextContainer?) {
    super.init(frame: frame, textContainer: textContainer)
    initialize()
}

private func initialize()
{
    // whatever
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM