我试图使CNMutableContact“可编码”。我已经构建了编码函数(见下文),但我得到了一些问题,以解码数组,如postalAddresses,emailAddresses等。
下面是我的encode函数:
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.contact.contactType.rawValue, forKey: .contactType)
try container.encode(self.contact.namePrefix, forKey: .namePrefix)
try container.encode(self.contact.givenName, forKey: .givenName)
try container.encode(self.contact.middleName, forKey: .middleName)
try container.encode(self.contact.familyName, forKey: .familyName)
try container.encode(self.contact.previousFamilyName, forKey: .previousFamilyName)
try container.encode(self.contact.nameSuffix, forKey: .nameSuffix)
try container.encode(self.contact.nickname, forKey: .nickname)
try container.encode(self.contact.jobTitle, forKey: .jobTitle)
try container.encode(self.contact.departmentName, forKey: .departmentName)
try container.encode(self.contact.organizationName, forKey: .organizationName)
var postalAddresses: [String:String] = [:]
self.contact.postalAddresses.forEach { postalAddress in
postalAddresses[postalAddress.label ?? "postal\(String(describing: index))"] = (CNPostalAddressFormatter.string(from: postalAddress.value, style: .mailingAddress))
}
try container.encode(postalAddresses, forKey: .postalAddresses)
var emailAddresses: [String:String] = [:]
self.contact.emailAddresses.forEach { emailAddress in
emailAddresses[emailAddress.label ?? "email\(String(describing: index))"] = (emailAddress.value as String)
}
try container.encode(emailAddresses, forKey: .emailAddresses)
var urlAddresses: [String:String] = [:]
self.contact.urlAddresses.forEach { urlAddress in
urlAddresses[urlAddress.label ?? "url\(String(describing: index))"] = (urlAddress.value as String)
}
try container.encode(urlAddresses, forKey: .urlAddresses)
var phoneNumbers: [String:String] = [:]
self.contact.phoneNumbers.forEach { phoneNumber in
phoneNumbers[phoneNumber.label ?? "phone\(String(describing: index))"] = phoneNumber.value.stringValue
}
try container.encode(phoneNumbers, forKey: .phoneNumbers)
var socialProfiles: [String:String] = [:]
self.contact.socialProfiles.forEach { socialProfile in
socialProfiles[socialProfile.label ?? "social\(String(describing: index))"] = socialProfile.value.urlString
}
try container.encode(socialProfiles, forKey: .socialProfiles)
try container.encode(self.contact.birthday, forKey: .birthday)
try container.encode(self.contact.note, forKey: .note)
}
如您所见,我以如下方式对postalAddresses进行编码:
var postalAddresses: [String:String] = [:]
self.contact.postalAddresses.forEach { postalAddress in
postalAddresses[postalAddress.label ?? "postal\(String(describing: index))"] = (CNPostalAddressFormatter.string(from: postalAddress.value, style: .mailingAddress))
}
try container.encode(postalAddresses, forKey: .postalAddresses)
但是我很难理解如何解码,下面是我的解码函数(不完整):
init(from decoder: Decoder) throws {
let decodedContact = try decoder.container(keyedBy: CodingKeys.self)
id = try decodedContact.decode(UUID.self, forKey: .id)
contactIdentifier = try decodedContact.decode(String.self, forKey: .contactIdentifier)
contact = CNMutableContact()
var intContactType = try decodedContact.decode(Int.self, forKey: .contactType)
if intContactType == 0 {
contact.contactType = CNContactType.person
} else {
contact.contactType = CNContactType.organization
}
contact.namePrefix = try decodedContact.decode(String.self, forKey: .namePrefix)
contact.givenName = try decodedContact.decode(String.self, forKey: .givenName)
contact.middleName = try decodedContact.decode(String.self, forKey: .middleName)
contact.familyName = try decodedContact.decode(String.self, forKey: .familyName)
contact.previousFamilyName = try decodedContact.decode(String.self, forKey: .previousFamilyName)
contact.nameSuffix = try decodedContact.decode(String.self, forKey: .nameSuffix)
contact.nickname = try decodedContact.decode(String.self, forKey: .nickname)
contact.jobTitle = try decodedContact.decode(String.self, forKey: .jobTitle)
contact.departmentName = try decodedContact.decode(String.self, forKey: .departmentName)
contact.organizationName = try decodedContact.decode(String.self, forKey: .organizationName)
// MISSING ARRAYS
let postalAddresses = try decodedContact.decode([String:String], forKey: .postalAddresses)
contact.birthday = try decodedContact.decode(DateComponents.self, forKey: .birthday)
contact.note = try decodedContact.decode(String.self, forKey: .note)
}
注意:decode函数返回postalAddresses解码行的错误。
你能帮助我理解我的方法是否正确以及如何解码数组吗?
谢谢
我尝试了不同的方法来解码postalAddresses,但总是得到一个错误。
2条答案
按热度按时间e4yzc0pl1#
没有必要重新发明轮子。
CN(Mutable)Contact
符合NSSecureCoding
,它可以序列化为Data
。在Swift中,有一个PropertyWrapper模式,它公开了示例,并可以在幕后执行编码/解码操作
在结构声明中
guz6ccqo2#
我终于改变了主意,因为更多的问题沿着了,我创建了一个结构,复制CNMutableContact和两个函数转换和CNMutableContact.你会发现完整的实现如下.
请随意评论,我在斯威夫特还是个新手