如何在 Swift IOS 中使用 NSKeyedArchiver 保存子 类 的对象
How to save objects with sub-classes using NSKeyedArchiver in Swift IOS
我只是尝试在 Swift 中编码,我正在尝试在 Apple Dev Library 中修改这个现有项目:Meals。
我希望在主餐中加入额外的子class,例如成分class,将其作为数组或成分。
import UIKit
import os.log
class Meal: NSObject, NSCoding {
//MARK: Properties
var name: String
var photo: UIImage?
var rating: Int
var recipe: [ingredients]?
//MARK: Archiving Paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("meals")
//MARK: Types
struct PropertyKey {
static let name = "name"
static let photo = "photo"
static let rating = "rating"
static let recipe = "recipe"
}
//MARK: Initialization
init?(name: String, photo: UIImage?, rating: Int, recipe: ingredients!) {
// The name must not be empty
guard !name.isEmpty else {
return nil
}
// The rating must be between 0 and 5 inclusively
guard (rating >= 0) && (rating <= 5) else {
return nil
}
// Initialization should fail if there is no name or if the rating is negative.
if name.isEmpty || rating < 0 {
return nil
}
// Initialize stored properties.
self.name = name
self.photo = photo
self.rating = rating
self.recipe = recipe
}
//MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(rating, forKey: PropertyKey.rating)
aCoder.encode(recipe, forKey: PropertyKey.recipe)
}
required convenience init?(coder aDecoder: NSCoder) {
// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Meal object.", log: OSLog.default, type: .debug)
return nil
}
// Because photo is an optional property of Meal, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let rating = aDecoder.decodeInteger(forKey: PropertyKey.rating)
let recipe = aDecoder.decodeObject(forKey: PropertyKey.recipe)
// Must call designated initializer.
self.init(name: name, photo: photo, rating: rating, recipe: recipe)
}
}
保存和加载餐点的函数调用如下:
private func saveMeals() {
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(meals, toFile: Meal.ArchiveURL.path)
if isSuccessfulSave {
os_log("Meals successfully saved.", log: OSLog.default, type: .debug)
} else {
os_log("Failed to save meals...", log: OSLog.default, type: .error)
}
}
private func loadMeals() -> [Meal]? {
return NSKeyedUnarchiver.unarchiveObject(withFile: Meal.ArchiveURL.path) as? [Meal]
}
我声明了一个新的 Class Ingredients.swift 来捕获我想要的成分。
import UIKit
import os.log
class Ingredients: NSObject {
struct PropertyKey {
static let name = "name"
static let quantity = "quantity"
}
var name: String!
var quantity: Double!
//MARK: Initialization
init?(name: Int, quantity: Double) {
self.name = name
self.quantity = quantity
}
}
我现在遇到的问题是 XCode 正在抛出“由于未捕获的异常而终止应用程序
'NSInvalidArgumentException', reason: '-[MealTracker.Ingredients encodeWithCoder:]: unrecognized selector sent to instance
0x1c40a0e40'"
请问如何成功地将 Ingredients 数组包含到保存的对象中?
您必须实施 NSCoding
协议
func encode(with aCoder: NSCoder) {}
required convenience init?(coder aDecoder: NSCoder) {}
内在习俗类也
class Ingredients: NSObject , NSCoding {}
我只是尝试在 Swift 中编码,我正在尝试在 Apple Dev Library 中修改这个现有项目:Meals。
我希望在主餐中加入额外的子class,例如成分class,将其作为数组或成分。
import UIKit
import os.log
class Meal: NSObject, NSCoding {
//MARK: Properties
var name: String
var photo: UIImage?
var rating: Int
var recipe: [ingredients]?
//MARK: Archiving Paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("meals")
//MARK: Types
struct PropertyKey {
static let name = "name"
static let photo = "photo"
static let rating = "rating"
static let recipe = "recipe"
}
//MARK: Initialization
init?(name: String, photo: UIImage?, rating: Int, recipe: ingredients!) {
// The name must not be empty
guard !name.isEmpty else {
return nil
}
// The rating must be between 0 and 5 inclusively
guard (rating >= 0) && (rating <= 5) else {
return nil
}
// Initialization should fail if there is no name or if the rating is negative.
if name.isEmpty || rating < 0 {
return nil
}
// Initialize stored properties.
self.name = name
self.photo = photo
self.rating = rating
self.recipe = recipe
}
//MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(rating, forKey: PropertyKey.rating)
aCoder.encode(recipe, forKey: PropertyKey.recipe)
}
required convenience init?(coder aDecoder: NSCoder) {
// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Meal object.", log: OSLog.default, type: .debug)
return nil
}
// Because photo is an optional property of Meal, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let rating = aDecoder.decodeInteger(forKey: PropertyKey.rating)
let recipe = aDecoder.decodeObject(forKey: PropertyKey.recipe)
// Must call designated initializer.
self.init(name: name, photo: photo, rating: rating, recipe: recipe)
}
}
保存和加载餐点的函数调用如下:
private func saveMeals() {
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(meals, toFile: Meal.ArchiveURL.path)
if isSuccessfulSave {
os_log("Meals successfully saved.", log: OSLog.default, type: .debug)
} else {
os_log("Failed to save meals...", log: OSLog.default, type: .error)
}
}
private func loadMeals() -> [Meal]? {
return NSKeyedUnarchiver.unarchiveObject(withFile: Meal.ArchiveURL.path) as? [Meal]
}
我声明了一个新的 Class Ingredients.swift 来捕获我想要的成分。
import UIKit
import os.log
class Ingredients: NSObject {
struct PropertyKey {
static let name = "name"
static let quantity = "quantity"
}
var name: String!
var quantity: Double!
//MARK: Initialization
init?(name: Int, quantity: Double) {
self.name = name
self.quantity = quantity
}
}
我现在遇到的问题是 XCode 正在抛出“由于未捕获的异常而终止应用程序
'NSInvalidArgumentException', reason: '-[MealTracker.Ingredients encodeWithCoder:]: unrecognized selector sent to instance 0x1c40a0e40'"
请问如何成功地将 Ingredients 数组包含到保存的对象中?
您必须实施 NSCoding
协议
func encode(with aCoder: NSCoder) {}
required convenience init?(coder aDecoder: NSCoder) {}
内在习俗类也
class Ingredients: NSObject , NSCoding {}