我需要获取商家的数据并将其显示在TableView中,我还想保留数据,但我仍然对我应该使用什么方法感到困惑。 也想要问一下这两种方法的区别,特别是它们的优点。
这一个:
class Merchant: NSObject {
var merchantID : NSNumber?
var merchantName : String?
var merchantaddress : String?
var contact_person : String?
var mobile_number : NSNumber?
var merchantImage : String?
var products : Product?
init?(merchantID: NSNumber, merchantName: String, merchantAddress: String, contactPerson: String, mobileNumber: NSNumber, merchantImage: String, products: Product){
self.merchantID = merchantID
self.merchantName = merchantName
self.merchantaddress = merchantAddress
self.contact_person = contactPerson
self.mobile_number = mobileNumber
self.merchantImage = merchantImage
self.products = products
super.init()
}
}
和这一个:
class Merchant: NSObject, NSCoding {
var merchantID : NSNumber?
var merchantName : String?
var merchantAddress : String?
var contactPerson : String?
var mobileNumber : NSNumber?
var merchantImage : String?
var products : Product?
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("merchant")
// MARK: Types
struct PropertyKey {
static let merchantIDKey = "merchant_id"
static let merchantNameKey = "merchant_name"
static let merchantaddressKey = "merchant_address"
static let merchant_contpersonKey = "contact_person"
static let merchantmobile_numberkey = "mobile_number"
static let merchantImageKey = "merchant_image"
static let productKey = "persons"
}
init?(merchantID: NSNumber, merchantName: String, merchantAddress: String, contactPerson: String, mobileNumber: NSNumber, merchantImage: String, products: Product){
self.merchantID = merchantID
self.merchantName = merchantName
self.merchantAddress = merchantAddress
self.contactPerson = contactPerson
self.mobileNumber = mobileNumber
self.merchantImage = merchantImage
self.products = products
super.init()
}
// MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(merchantID, forKey: PropertyKey.merchantIDKey)
aCoder.encode(merchantName, forKey: PropertyKey.merchantNameKey)
aCoder.encode(merchantAddress, forKey: PropertyKey.merchantaddressKey)
aCoder.encode(contactPerson, forKey: PropertyKey.merchant_contpersonKey)
aCoder.encode(mobileNumber, forKey: PropertyKey.merchantmobile_numberkey)
aCoder.encode(merchantImage, forKey: PropertyKey.merchantImageKey)
aCoder.encode(products, forKey: PropertyKey.productKey)
}
required convenience init?(coder aDecoder: NSCoder) {
let merchantid = aDecoder.decodeObject(forKey: PropertyKey.merchantIDKey) as! NSNumber
let merchantname = aDecoder.decodeObject(forKey: PropertyKey.merchantNameKey) as! String
let merchantaddress = aDecoder.decodeObject(forKey: PropertyKey.merchantaddressKey) as! String
let contactperson = aDecoder.decodeObject(forKey: PropertyKey.merchant_contpersonKey) as! String
let mobilenumber = aDecoder.decodeObject(forKey: PropertyKey.merchantmobile_numberkey) as! NSNumber
let merchantimage = aDecoder.decodeObject(forKey: PropertyKey.merchantImageKey) as! String
let product = aDecoder.decodeObject(forKey: PropertyKey.productKey) as! Product
// Must call designated initializer.
self.init(merchantID: merchantid, merchantName: merchantname, merchantAddress: merchantaddress, contactPerson : contactperson, mobileNumber: mobilenumber, merchantImage: merchantimage, products: product)
}
@discardableResult func loadSender() -> [Merchant]? {
return NSKeyedUnarchiver.unarchiveObject(withFile: Merchant.ArchiveURL.path) as? [Merchant]
}
}
我是Swift的初学者 谢谢