2014-09-09 16 views
9

Tôi đang cố gắng sử dụng giao thức NSCoding trên lớp tôi đã viết nhanh, nhưng dường như không thể hiểu tại sao trình biên dịch phàn nàn rằng nó "không tuân theo giao thức NSCoding" khi tôi thực hiện các phương thức được yêu cầu:Swift: Không tuân theo giao thức NSCoding

class ServerInfo: NSObject, NSCoding { 

    var username = "" 
    var password = "" 
    var domain = "" 
    var location = "" 
    var serverFQDN = "" 
    var serverID = "" 

    override init() { 

    } 

    init(coder aDecoder: NSCoder!) { 
     self.username = aDecoder.decodeObjectForKey("username") as NSString 
     self.password = aDecoder.decodeObjectForKey("password") as NSString 
     self.domain = aDecoder.decodeObjectForKey("domain") as NSString 
     self.location = aDecoder.decodeObjectForKey("location") as NSString 
     self.serverFQDN = aDecoder.decodeObjectForKey("serverFQDN") as NSString 
     self.serverID = aDecoder.decodeObjectForKey("serverID") as NSString 
    } 


    func encodeWithCoder(_aCoder: NSCoder!) { 
     _aCoder.encodeObject(self.username, forKey: "username") 
     _aCoder.encodeObject(self.password, forKey: "password") 
     _aCoder.encodeObject(self.domain, forKey: "domain") 
     _aCoder.encodeObject(self.location, forKey: "location") 
     _aCoder.encodeObject(self.serverFQDN, forKey: "serverFQDN") 
     _aCoder.encodeObject(self.serverID, forKey: "serverID") 
    } 

} 

Đây có phải là lỗi hoặc tôi chỉ thiếu một cái gì đó?

Trả lời

17

Như bạn có thể thấy trong các thông điệp biên dịch chi tiết trong Báo cáo navigator, phương pháp của bạn không được công bố một cách chính xác:

 
error: type 'ServerInfo' does not conform to protocol 'NSCoding' 
class ServerInfo: NSObject, NSCoding { 
^ 
Foundation.NSCoding:2:32: note: protocol requires function 'encodeWithCoder' with type '(NSCoder) -> Void' 
    @objc(encodeWithCoder:) func encodeWithCoder(aCoder: NSCoder) 
          ^
note: candidate has non-matching type '(NSCoder!) ->()' 
    func encodeWithCoder(_aCoder: NSCoder!) { 
     ^
Foundation.NSCoding:3:25: note: protocol requires initializer 'init(coder:)' with type '(coder: NSCoder)' 
    @objc(initWithCoder:) init(coder aDecoder: NSCoder) 
         ^
note: candidate has non-matching type '(coder: NSCoder!)' 
    init(coder aDecoder: NSCoder!) { 

(. Điều này có thể thay đổi giữa các phiên bản beta) Bên cạnh đó, initWithCoder phương pháp đã được đánh dấu là required:

required init(coder aDecoder: NSCoder) { } 

func encodeWithCoder(_aCoder: NSCoder) { } 

Trong Swift 3 các phương pháp cần thiết là

required init(coder aDecoder: NSCoder) { } 

func encode(with aCoder: NSCoder) { } 
+2

"Điều này có thể đã thay đổi giữa bản phát hành beta" Có, một người muốn hét lên với Swift, "Dude, tôi có chữ ký của phương thức này từ bạn! _ Sao anh dám nói với tôi rằng giờ họ đã sai? " Nhưng tất nhiên đó là cuộc sống trong làn beta tốc độ nhanh ... – matt

+0

@matt: Ít nhất trình biên dịch in một thông báo hợp lý ở đây, thay vì * "$% §? & Không thể chuyển thành UInt8" * :) –

+0

Ngoại trừ một nửa thời gian tham số mà nó than phiền là sai! :)) – matt

4

Các thông số không ngầm nào, và khởi tạo yêu cầu required modifier (tháo!):

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

Đối Swift 3

Một thay đổi nhỏ nhưng quan trọng đã được cam kết. Phương thức init giống nhau nhưng phương thức encodeWithCoder đã được sửa đổi.

required init(coder aDecoder: NSCoder) { 
    ... 
    } 

    func encode(with _aCoder: NSCoder) { 
    ... 
    } 
0

Đối Swift 3 (trên Xcode 8.2 beta (8C23))

Nó dường như đã thay đổi một lần nữa. Đây là biến thể duy nhất mà tôi có thể làm việc ...

func encodeWithCoder(_ _aCoder: NSCoder) { 
    ... 
} 
Các vấn đề liên quan