2017-07-26 37 views
6

Tôi đã struct sau ...Swift Codable giao thức ... mã hóa/giải mã lớp NSCoding

struct Photo: Codable { 

    let hasShadow: Bool 
    let image: UIImage? 

    enum CodingKeys: String, CodingKey { 
     case `self`, hasShadow, image 
    } 

    init(hasShadow: Bool, image: UIImage?) { 
     self.hasShadow = hasShadow 
     self.image = image 
    } 

    init(from decoder: Decoder) throws { 
     let container = try decoder.container(keyedBy: CodingKeys.self) 
     hasShadow = try container.decode(Bool.self, forKey: .hasShadow) 

     // This fails 
     image = try container.decode(UIImage?.self, forKey: .image) 
    } 

    func encode(to encoder: Encoder) throws { 
     var container = encoder.container(keyedBy: CodingKeys.self) 
     try container.encode(hasShadow, forKey: .hasShadow) 

     // This also fails 
     try container.encode(image, forKey: .image) 
    } 
} 

Encoding một Photo không thành công với ...

Tùy chọn không phù hợp với Encodable vì UIImage không không phù hợp với Mã hóa

Giải mã không thành công với…

chính không tìm thấy khi chờ đợi loại không bắt buộc bắt buộc cho mã chủ chốt \ "hình ảnh \" "))

Có cách nào để mã hóa đối tượng Swift bao gồm NSObject thuộc tính lớp con phù hợp với NSCoding (UIImage, UIColor, v.v ...)?

+3

Bạn phải viết mã hóa tùy chỉnh/code giải mã để lưu trữ/huỷ lưu trữ các đối tượng đến và đi từ 'dữ liệu'. Vui lòng đọc [Loại mã hóa và giải mã tùy chỉnh] (https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types) – vadian

Trả lời

7

Nhờ @vadian đã chỉ cho tôi theo hướng mã hóa/giải mã Data ...

class Photo: Codable { 

    let hasShadow: Bool 
    let image: UIImage? 

    enum CodingKeys: String, CodingKey { 
     case `self`, hasShadow, imageData 
    } 

    init(hasShadow: Bool, image: UIImage?) { 
     self.hasShadow = hasShadow 
     self.image = image 
    } 

    required init(from decoder: Decoder) throws { 
     let container = try decoder.container(keyedBy: CodingKeys.self) 
     hasShadow = try container.decode(Bool.self, forKey: .hasShadow) 

     if let imageData = try container.decodeIfPresent(Data.self, forKey: .imageData) { 
      image = NSKeyedUnarchiver.unarchiveObject(with: imageData) as? UIImage 
     } else { 
      image = nil 
     } 
    } 

    func encode(to encoder: Encoder) throws { 
     var container = encoder.container(keyedBy: CodingKeys.self) 
     try container.encode(hasShadow, forKey: .hasShadow) 

     if let image = image { 
      let imageData = NSKeyedArchiver.archivedData(withRootObject: image) 
      try container.encode(imageData, forKey: .imageData) 
     } 
    } 
} 
+1

Vì vậy, cuối cùng 'Codable' không thực sự dễ dàng hơn khi sử dụng" loại tùy chỉnh " ? : - | – d4Rk

+0

Vâng - nó cho phép bạn mã hóa/giải mã các lớp con không phải 'NSObject' (enums & structs) –

+0

@AshleyMills, tôi nhận được lỗi này "Loại 'Ảnh' không phù hợp với giao thức 'Có thể giải mã'" trong khi sao chép mã này trong tập tin. –

Các vấn đề liên quan