Swift: Does not conform to protocol NSCoding

前端 未结 3 1940
一个人的身影
一个人的身影 2020-12-17 17:49

I am trying to use the NSCoding protocol on a class I have written in swift, but cannot seem to figure out why the compiler complains that it \"does not conform to protocol

3条回答
  •  [愿得一人]
    2020-12-17 18:11

    The parameters are not implicitly unwrapped (remove the !), and the initializer requires the required modifier:

    required init(coder aDecoder: NSCoder) {
    ...
    func encodeWithCoder(_aCoder: NSCoder) {
    

    For Swift 3

    A minor but important change has been commited . The init method is same but the encodeWithCoder method has been modified.

       required init(coder aDecoder: NSCoder) {
        ...
       }
    
       func encode(with _aCoder: NSCoder) { 
       ...
       }
    

提交回复
热议问题