2

I'm making a simple dictionary, [ABPropertyID : String ]:

let propertyToMethod = [ 
    kABPersonInstantMessageProperty : "contactMethodInstantMessage",
    kABPersonEmailProperty : "contactMethodEmail",
    kABPersonPhoneProperty : "contactMethodPhoneCall" ]

This crashes at runtime with Dictionary literal contains duplicate keys. Seems like the keys of type ABPropertyID aren't resolved until runtime...? But either way, why would there be a duplicate among them when they're used to differentiate address book properties?

And also, why would they all be zero?:

println("kabIM: \(kABPersonInstantMessageProperty), kabEmail: \(kABPersonEmailProperty), kabPhone: \(kABPersonPhoneProperty)")

prints kabIM: 0, kabEmail: 0, kabPhone: 0.

I was working under the impression that they were compile-time constants with unique values. This is how they are declared in the library:

let kABPersonEmailProperty: ABPropertyID // Email(s) - kABMultiStringPropertyType

...

typealias ABPropertyID = Int32

Running on iphone 5, 8.3.

Andreas
  • 2,665
  • 2
  • 29
  • 38

1 Answers1

2

My original code compiles, but for some reason these are runtime "constants" that change values from 0 to something else at an unknown point.

I postponed the construction of the dictionary to after initialization of my object, and it works.

var propertyToMethod: [ABPropertyID : String] { get {
    println("kabIM: \(kABPersonInstantMessageProperty), kabEmail: \(kABPersonEmailProperty), kabPhone: \(kABPersonPhoneProperty)")
    return [ kABPersonInstantMessageProperty : contactMethodInstantMessage,
    kABPersonEmailProperty : contactMethodEmail,
    kABPersonPhoneProperty : contactMethodPhoneCall ] }
}

But I still can't be sure that it won't crash in the future...

Andreas
  • 2,665
  • 2
  • 29
  • 38
  • 1
    According to the "duplicate" that I linked to, you have to call one of the address book "create" function before using these "constants", e.g. `ABAddressBookCreateWithOptions()`. – Martin R May 25 '15 at 14:13
  • Thanks! I looked at that answer but couldn't find the quoted paragraph in the linked documentation. – Andreas May 25 '15 at 14:15
  • I cannot find it either. But the linked-to answer seems still to be correct, after calling `ABAddressBookCreateWithOptions()` the ""constants"" evaluate to non-zero numbers. – Martin R May 25 '15 at 14:24