我正在尝试加载我的应用程序的联系人。它在模拟器中运行良好。但在 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)
我认为这是由于联系人中的姓名所致,如果我尝试获取电话号码,它工作正常。我可以看到所有电话号码,但对于姓名,我可以看到大约 350 个联系人,然后应用程序崩溃。
知道我该如何解决这个问题吗?
要考虑潜在的零值(当联系人的记录缺少姓名时可能会发生这种情况),请更改
let currentContactName = ABRecordCopyCompositeName(currentContact).takeRetainedValue() as String
to
let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue() as? String
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)