I decided to continue my remaining project with Swift-language.When I added the custom class (.swift class which sub class to UIViewcontroller) in my storyboard viewcontroller and loaded the project the app crash suddenly with following error:
我决定用swift语言继续我剩下的项目。当我添加自定义类(。swift类是UIViewcontroller的子类)在我的storyboard viewcontroller中然后载入项目app会突然崩溃,错误如下:
fatal error: use of unimplemented initializer 'init(coder:)' for class
致命错误:对类使用未实现的初始化器'init(coder:)'
This is a code :
这是一个代码:
import UIKit
class TestViewController: UIViewController {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
Please suggest something,
请建议,
5 个解决方案
#1
191
Issue
This is caused by the absence of the initializer init?(coder aDecoder: NSCoder)
on the target UIViewController
. That method is required because instantiating a UIViewController
from a UIStoryboard
calls it.
这是由初始化器init的缺失引起的?(编码器加码器:NSCoder)目标UIViewController。这个方法是必需的,因为从UIStoryboard中实例化一个UIViewController会调用它。
To see how we initialize a UIViewController
from a UIStoryboard
, please take a look here
要了解如何从uistoryboard上初始化一个UIViewController,请看这里
Why is this not a problem with Objective-C?
Because Objective-C automatically inherits all the required UIViewController
initializers.
因为Objective-C自动继承所有必需的UIViewController初始化器。
Why doesn't Swift automatically inherit the initializers?
Swift by default does not inherit the initializers due to safety. But it will inherit all the initializers from the superclass if all the properties have a value (or optional) and the subclass has not defined any designated initializers.
由于安全性,Swift默认不继承初始化器。但是,如果所有属性都具有一个值(或可选),并且子类没有定义任何指定的初始化器,那么它将继承父类的所有初始化器。
Solution
1. First method
Manually implementing init?(coder aDecoder: NSCoder)
on the target UIViewController
手动执行初始化?(编码器加码器:NSCoder)目标UIViewController
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
2. Second method
Removing init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
on your target UIViewController
will inherit all of the required initializers from the superclass as Dave Wood pointed on his answer below
删除init(nibName nibNameOrNil:字符串?在你的目标UIViewController上,将继承父类中所有必需的初始化器,就像Dave Wood在下面的回答中指出的那样
#2
25
Another option besides @3r1d's is to instead remove the following init method from your class:
除了@3r1d之外的另一个选项是,从您的类中删除下面的init方法:
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization
}
Including that init method, prevents the sub class from inheriting the init(coder aDecoder: NSCoder!)
from its super class. By not including it, your class will inherit both.
包括init方法,防止子类从它的超类继承init(coder aDecoder: NSCoder!)。通过不包含它,您的类将继承这两者。
Note: See WWDC 2014 Session 403 "Intermediate Swift" at about the 33:50 mark for more details.
注意:参见WWDC 2014年第403届“中级Swift”,时间大约在33:50。
#3
9
For people having the same issue with swift UICollectionViewCells
, add the code that @3r1d suggested to your custom UICollectionViewCell
class and not to the View Controller:
对于使用swift UICollectionViewCells的人来说,添加@3r1d建议的代码到您的自定义UICollectionViewCell类,而不添加到视图控制器:
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
#4
3
For those needing the code in Swift:
对于需要Swift代码的用户:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
[Edit] This was for an older version of Swift. Possibly doesn't work anymore.
[编辑]这是Swift的旧版本。可能不起作用了。
#5
0
Rather than adding some methods for making internal mechanism work fine, i would go with defining my attributes as @lazy and initialise them right in the class scope.
与其添加一些使内部机制正常工作的方法,不如将属性定义为@lazy,并在类范围内初始化它们。
#1
191
Issue
This is caused by the absence of the initializer init?(coder aDecoder: NSCoder)
on the target UIViewController
. That method is required because instantiating a UIViewController
from a UIStoryboard
calls it.
这是由初始化器init的缺失引起的?(编码器加码器:NSCoder)目标UIViewController。这个方法是必需的,因为从UIStoryboard中实例化一个UIViewController会调用它。
To see how we initialize a UIViewController
from a UIStoryboard
, please take a look here
要了解如何从uistoryboard上初始化一个UIViewController,请看这里
Why is this not a problem with Objective-C?
Because Objective-C automatically inherits all the required UIViewController
initializers.
因为Objective-C自动继承所有必需的UIViewController初始化器。
Why doesn't Swift automatically inherit the initializers?
Swift by default does not inherit the initializers due to safety. But it will inherit all the initializers from the superclass if all the properties have a value (or optional) and the subclass has not defined any designated initializers.
由于安全性,Swift默认不继承初始化器。但是,如果所有属性都具有一个值(或可选),并且子类没有定义任何指定的初始化器,那么它将继承父类的所有初始化器。
Solution
1. First method
Manually implementing init?(coder aDecoder: NSCoder)
on the target UIViewController
手动执行初始化?(编码器加码器:NSCoder)目标UIViewController
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
2. Second method
Removing init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
on your target UIViewController
will inherit all of the required initializers from the superclass as Dave Wood pointed on his answer below
删除init(nibName nibNameOrNil:字符串?在你的目标UIViewController上,将继承父类中所有必需的初始化器,就像Dave Wood在下面的回答中指出的那样
#2
25
Another option besides @3r1d's is to instead remove the following init method from your class:
除了@3r1d之外的另一个选项是,从您的类中删除下面的init方法:
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization
}
Including that init method, prevents the sub class from inheriting the init(coder aDecoder: NSCoder!)
from its super class. By not including it, your class will inherit both.
包括init方法,防止子类从它的超类继承init(coder aDecoder: NSCoder!)。通过不包含它,您的类将继承这两者。
Note: See WWDC 2014 Session 403 "Intermediate Swift" at about the 33:50 mark for more details.
注意:参见WWDC 2014年第403届“中级Swift”,时间大约在33:50。
#3
9
For people having the same issue with swift UICollectionViewCells
, add the code that @3r1d suggested to your custom UICollectionViewCell
class and not to the View Controller:
对于使用swift UICollectionViewCells的人来说,添加@3r1d建议的代码到您的自定义UICollectionViewCell类,而不添加到视图控制器:
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
#4
3
For those needing the code in Swift:
对于需要Swift代码的用户:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
[Edit] This was for an older version of Swift. Possibly doesn't work anymore.
[编辑]这是Swift的旧版本。可能不起作用了。
#5
0
Rather than adding some methods for making internal mechanism work fine, i would go with defining my attributes as @lazy and initialise them right in the class scope.
与其添加一些使内部机制正常工作的方法,不如将属性定义为@lazy,并在类范围内初始化它们。