我试图从地址簿中检索一些条目但是却遇到了“公司”的问题。名称值。
如果地址簿中的联系人已完成公司,则可以正常工作。但是如果联系人没有崩溃应用程序(线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)。
我尝试了各种方法来检测nil,但似乎没有一种方法可行。这部分代码: -
func peoplePickerNavigationController(
peoplePicker: ABPeoplePickerNavigationController!,
didSelectPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
let addresses: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()
let indx = Int(identifier) as CFIndex
let address: NSDictionary = ABMultiValueCopyValueAtIndex(addresses,
indx).takeRetainedValue() as NSDictionary
if address[kABPersonAddressZIPKey as String] != nil {
postcode = address[kABPersonAddressZIPKey as String] as String
}
if address[kABPersonAddressCountryKey as String] != nil {
country = address[kABPersonAddressCountryKey as String] as String
}
if address[kABPersonAddressStateKey as String] != nil {
county = address[kABPersonAddressStateKey as String] as String
}
if address[kABPersonAddressCityKey as String] != nil {
city = address[kABPersonAddressCityKey as String] as String
}
if address[kABPersonAddressStreetKey as String] != nil {
street = address[kABPersonAddressStreetKey as String] as String
}
let recordType = ABRecordCopyValue(person, kABPersonOrganizationProperty)
if recordType != nil {
company = ABRecordCopyValue(person, kABPersonOrganizationProperty)?.takeRetainedValue() as String? ?? ""
} else {
println("just a person, not company")
}
有什么建议吗?
非常感谢,
安东尼