2017-04-03 64 views
0

是否有可能在Swift中创建可选的初始化参数,以便我可以使用从API调用返回的值从JSON创建对象,但随后当我保存该对象时,我也可以保存下载的UIImage是我之前获得的其中一个网址。Swift 3可选参数

例子:

class Story: NSObject, NSCoding { 
     var id: Int? 
     var title, coverImageURL: String? 
     var coverImage: UIImage? 

    required init?(anId: Int?, aTitle: String?, aCoverImageURL: String?) { 
      self.id = anId 
      self.title = aTitle 
      self.coverImageURL = aCoverImageURL 
    } 
    convenience init?(json: [String: Any]) { 
      let id = json["id"] as? Int 
      let title = json["title"] as? String 
      let coverImageURL = json["cover_image"] as? String 

      self.init(
       anId: id, 
       aTitle: title, 
       aCoverImageURL: coverImageURL, 
      ) 
     } 

再后来我想对象保存到内存

//MARK: Types 
    struct PropertyKey { 
     static let id = "id" 
     static let title = "title" 
     static let coverImageURL = "coverImageURL" 
     static let coverImage = "coverImage" 
    } 

    //MARK: NSCoding 
    func encode(with aCoder: NSCoder) { 
     aCoder.encode(id, forKey: PropertyKey.id) 
     aCoder.encode(title, forKey: PropertyKey.title) 
     aCoder.encode(coverImageURL, forKey: PropertyKey.coverImageURL) 
     aCoder.encode(coverImage, forKey: PropertyKey.coverImage) 
    } 

    required convenience init?(coder aDecoder: NSCoder) { 
     guard let id = aDecoder.decodeObject(forKey: PropertyKey.id) as? Int else { 
      os_log("Unable to decode the id for a Story object.", log: OSLog.default, type: .debug) 
      return nil 
     } 
     guard let title = aDecoder.decodeObject(forKey: PropertyKey.title) as? String else { 
      os_log("Unable to decode the title for a Story object.", log: OSLog.default, type: .debug) 
      return nil 
     } 

     let coverImageURL = aDecoder.decodeObject(forKey: PropertyKey.coverImageURL) as? String 
     let coverImage = aDecoder.decodeObject(forKey: PropertyKey.coverImage) as? UIImage 

     self.init(
      anId: id, 
      aTitle: title, 
      aCoverImageURL: coverImageURL, 
      coverImage: coverImage, 
     ) 
    } 

这是否有道理?我希望能够在获得API响应后立即保存Story对象,但后来当我将故事保存到内存中时,我希望能够保存为coverImage提取的UIImage。

我该怎么做?

+1

所以只有coverImage应该是可选的 –

+0

顺便说一句你需要将你的UIImage转换为Data以便能够将其保存到磁盘(编码) –

+0

@LeoDabus正确。 – Arel

回答

0

我不确定为什么没有人在这个答案上简单点,但答案是简单地使您的属性选项,然后你可以设置它们的值或零。如果需要,您还可以创建便利初始值设定项,将某些值自动设置为零。所以,以我的应用程序为例,我有一个从API调用构建的模型。该模型的值为id,created_at等等,直到将记录保存到服务器时才存在,但我在本地创建对象,存储它们并最终将它们发送到服务器,所以我需要能够设置上面创建从JSON对象,只有当,所以这里的值是我做过什么:

class Story: NSObject, NSCoding { 
     var id: Int? 
     var title, coverImageURL: String? 
     var coverImage: UIImage? 

    required init?(anId: Int?, aTitle: String?, aCoverImageURL: String?) { 
      self.id = anId 
      self.title = aTitle 
      self.coverImageURL = aCoverImageURL 
    } 
    convenience init?(json: [String: Any]) { 
      let id = json["id"] as? Int 
      let title = json["title"] as? String 
      let coverImageURL = json["cover_image"] as? String 

      self.init(
       anId: id, 
       aTitle: title, 
       aCoverImageURL: coverImageURL, 
      ) 
     } 

     convenience init?(aTitle: String, aCoverImage: UIImage?) { 
      let title = aTitle 
      let subtitle = aSubtitle 
      let coverImage = aCoverImage 
      let isActive = activeStatus 

      self.init(
       anId: nil, 
       aTitle: title, 
       aCoverImageURL: nil, 
       aCoverImage: coverImage, 
      ) 
     } 

正如你所看到的,我只设置两个值的时候,我在本地创建一个对象,而其他值只需设置为nil。要允许将值设置为nil,只需在设置时将其设置为可选。简单!