Swift 3可选参数
是否可以在Swift中创建可选的初始化参数,以便我可以使用API调用返回的值从JSON创建一个对象,但是当我稍后保存该对象时,我还可以保存下载的UIImage,以用于我之前获得的一个URL 例如:Swift 3可选参数,swift,swift3,Swift,Swift3,是否可以在Swift中创建可选的初始化参数,以便我可以使用API调用返回的值从JSON创建一个对象,但是当我稍后保存该对象时,我还可以保存下载的UIImage,以用于我之前获得的一个URL 例如: class Story: NSObject, NSCoding { var id: Int? var title, coverImageURL: String? var coverImage: UIImage? required init?
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获得响应后立即保存一个故事对象,但稍后当我将故事保存到内存时,我希望能够将获取的UIImage保存到coverImage
我该怎么做 我不知道为什么没有人接受这个答案的简单要点,但答案是简单地将您的属性设置为可选,然后您可以将它们设置为一个值,或nil。您还可以创建方便的初始值设定项,如果需要,可以自动将某些值设置为nil。因此,以我的应用程序为例,我有一个通过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
,只需将其设置为可选值即可。简单 所以只有coverImage应该是可选的。顺便说一句,您需要将UIImage转换为数据才能将其保存到磁盘(encode)@LeoDabus correct。我认为将映像添加到类中不是一个好主意。下载完成后,最好只为其本地fileURL添加一个属性。remoteURL和localURL。为什么不创建一个结构而不是一个类,如果需要持久化,只需使用服务器提供的相同格式将其保存为json字符串