从 iPhone 加载联系人在 Swift 中崩溃
Loading contacts from iPhone crashes in Swift
我正在尝试为我的应用程序加载联系人。它在模拟器中运行良好。但是在 iPhone 中崩溃了。
我使用的代码:
func getContactNames()
{
let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
for record in allContacts {
let currentContact: ABRecordRef = record
let currentContactName = ABRecordCopyCompositeName(currentContact).takeRetainedValue() as String
if(currentContactName != "") {
println("found \(currentContactName).")
}
}
}
此功能正常运行,在获得少量联系人后,应用程序崩溃并显示日志:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
我认为这是由于联系人中的姓名,如果我尝试获取 phone 号码,它工作正常。我可以看到所有 phone 号码,但如果名字,我可以看到大约 350 个联系人,然后应用程序崩溃了。
知道如何解决这个问题吗?
要考虑潜在的零值(当联系人的记录缺少姓名时可能会发生),更改
let currentContactName = ABRecordCopyCompositeName(currentContact).takeRetainedValue() as String
至
let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue() as? String
使用上面的代码对我有用
func readAllPeopleInAddressBook(addressBook: ABAddressBookRef){
/* Get all the people in the address book */
let allPeople = ABAddressBookCopyArrayOfAllPeople(
addressBook).takeRetainedValue() as NSArray
for person: ABRecordRef in allPeople{
if(ABRecordCopyValue(person,
kABPersonFirstNameProperty) != nil){
let firstName = ABRecordCopyValue(person,
kABPersonFirstNameProperty).takeRetainedValue() as? String
println("First name = \(firstName)")
}
if (ABRecordCopyValue(person,
kABPersonLastNameProperty) != nil){
let lastName = ABRecordCopyValue(person,
kABPersonLastNameProperty).takeRetainedValue()as? String
println("Last name = \(lastName)")
}
}
}
我正在尝试为我的应用程序加载联系人。它在模拟器中运行良好。但是在 iPhone 中崩溃了。 我使用的代码:
func getContactNames()
{
let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
for record in allContacts {
let currentContact: ABRecordRef = record
let currentContactName = ABRecordCopyCompositeName(currentContact).takeRetainedValue() as String
if(currentContactName != "") {
println("found \(currentContactName).")
}
}
}
此功能正常运行,在获得少量联系人后,应用程序崩溃并显示日志:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
我认为这是由于联系人中的姓名,如果我尝试获取 phone 号码,它工作正常。我可以看到所有 phone 号码,但如果名字,我可以看到大约 350 个联系人,然后应用程序崩溃了。
知道如何解决这个问题吗?
要考虑潜在的零值(当联系人的记录缺少姓名时可能会发生),更改
let currentContactName = ABRecordCopyCompositeName(currentContact).takeRetainedValue() as String
至
let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue() as? String
使用上面的代码对我有用
func readAllPeopleInAddressBook(addressBook: ABAddressBookRef){
/* Get all the people in the address book */
let allPeople = ABAddressBookCopyArrayOfAllPeople(
addressBook).takeRetainedValue() as NSArray
for person: ABRecordRef in allPeople{
if(ABRecordCopyValue(person,
kABPersonFirstNameProperty) != nil){
let firstName = ABRecordCopyValue(person,
kABPersonFirstNameProperty).takeRetainedValue() as? String
println("First name = \(firstName)")
}
if (ABRecordCopyValue(person,
kABPersonLastNameProperty) != nil){
let lastName = ABRecordCopyValue(person,
kABPersonLastNameProperty).takeRetainedValue()as? String
println("Last name = \(lastName)")
}
}
}