2017-04-03 73 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,只需在設置時將其設置爲可選。簡單!