您可以为此使用 Core Data,但您可能需要先考虑一个更轻量级的解决方案。如果您使用的是 Swift 4,您可以使用 Encodable/Decodable/Codable 协议来序列化对象。
考虑以下几点:
如果您要在您的产品类上采用Codable 协议,那么您可以将您的对象从您的 API 反序列化到文件系统(示例中的文档目录)。由于 UIImage 不符合Codable 协议,您需要手动处理解码/编码为另一种类型(下例中为Data)。
struct Product: Codable {
var id: String?
var name: String?
var rate: Double?
var quantity: Int?
var sku: String?
var productCatalog: String?
var productDescription: String?
var images: [UIImage] = []
var mrp: String?
private enum CodingKeys: String, CodingKey {
case id
case name
case rate
case quantity
case sku
case productCatalog
case productDescription
case images
case mrp
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.name = try container.decode(String.self, forKey: .name)
self.rate = try container.decode(Double.self, forKey: .rate)
self.quantity = try container.decode(Int.self, forKey: .quantity)
self.sku = try container.decode(String.self, forKey: .sku)
self.productCatalog = try container.decode(String.self, forKey: .productCatalog)
self.productDescription = try container.decode(String.self, forKey: .productDescription)
self.mrp = try container.decode(String.self, forKey: .mrp)
if let imageData = try container.decodeIfPresent([Data].self, forKey: .images) {
imageData.forEach({ (data) in
if let image = UIImage(data: data) {
self.images.append(image)
}
})
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(id, forKey: .id)
try container.encodeIfPresent(name, forKey: .name)
try container.encodeIfPresent(rate, forKey: .rate)
try container.encodeIfPresent(quantity, forKey: .quantity)
try container.encodeIfPresent(sku, forKey: .sku)
try container.encodeIfPresent(productCatalog, forKey: .productCatalog)
try container.encodeIfPresent(productDescription, forKey: .productDescription)
try container.encodeIfPresent(mrp, forKey: .mrp)
var imageData = [Data]()
for image in images {
if let data = UIImagePNGRepresentation(image) {
imageData.append(data)
}
}
try container.encode(imageData, forKey: .images)
}
}
然后,您可以使用 JSON 或 PLIST 编码器来读取数据并将其写入磁盘:
struct ProductData {
var products: [Product] = []
var productsURL: URL {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
guard let url = urls.first else {
fatalError("Document Directory url invalid")
}
return url.appendingPathComponent("products.json")
}
mutating func loadProducts() {
products.removeAll()
guard let data = FileManager.default.contents(atPath: productsURL.path) else {
return
}
let decoder = JSONDecoder()
do {
products = try decoder.decode([Product].self, from: data)
} catch {
// Handle Error
}
}
mutating func saveProducts() {
let encoder = JSONEncoder()
let data: Data
do {
data = try encoder.encode(products)
} catch {
// Handle Error
return
}
do {
try data.write(to: productsURL)
} catch {
// Handle Error
}
}
}
自定义类型的编码和解码的其他信息可以在这里找到:Encoding and Decoding Custom Types