尝试使用Address Book API打印联系人时应用程序崩溃

时间:2014-10-23 13:04:30

标签: swift

我有以下代码来打印联系人:

let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray as [ABRecord]
for person in people {
    println(ABRecordCopyCompositeName(person).takeRetainedValue())
}

在迭代people数组后,程序总是崩溃。该数组有6个AnyObject类型的元素。这可能是演员问题吗?

我得到的错误是:Thread_1 EXC_BREAKPOINT (code = EXC_i386_BPT, subcode=0x0)

1 个答案:

答案 0 :(得分:0)

可能在其他地方发生了错误。我尝试下面的代码,它工作正常。

var addressBook: ABAddressBookRef? = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, nil))
ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in
     if success {
         let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray as [ABRecord]
         for person in people {
              println(ABRecordCopyCompositeName(person).takeRetainedValue())
         }
     }
})