1

Here is a better example:

let person: ABRecordRef = ABPersonCreate().takeRetainedValue()

let phoneNos = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()

if ABMultiValueGetCount(phoneNos) > 0 {
    print("yay") } else {
    print("nay") //you will never see this }

Always crashes.

let person: ABRecordRef = ABPersonCreate().takeRetainedValue()
//lets add a number then
let phoneNumbers: ABMutableMultiValue =
ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(phoneNumbers, "1234", kABPersonPhoneMainLabel, nil)
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumbers, nil)

let phoneNos = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()

if ABMultiValueGetCount(phoneNos) > 0 {

    print("yay")
} else {
    print("nay") //you will never see this
}

Works just fine. I need to check if there is a phone number, how the #$^% am I supposed to do that when ABRecordCopyValue can't return a nil without exploding.

  • Have you tried this: `http://stackoverflow.com/questions/24534436/check-if-abmultivalueref-is-has-no-values` ? – Marc-Alexandre Bérubé Nov 24 '15 at 19:55
  • I sure did. I couldn't see how it applied. They were crashing on no contact found instead of no number for contact so they copied it to an array to get the number of people. If I even touch `ABRecordCopyValue` with `kABPersonPhoneProperty` returning nil it crashes. – user0000001 Nov 24 '15 at 20:58

1 Answers1

0

The following will prevent the crash, if ABRecordCopyValue returns nil; but in case there will be a value returned, it will be stored in phoneNos:

if let phoneNos = ABRecordCopyValue(person, kABPersonPhoneProperty)?.takeRetainedValue()
{
     print("\(person)'s phone number: \(phoneNos)")
}
else
{
     print("nay")
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98