Swift - Checking unmanaged address book single value property for nil
Asked Answered
B

3

12

I'm relative new to iOS-Development and swift. But up to this point I was always able to help myself by some research on stackoverflow and several documentations and tutorials. However, there is a problem I couldn't find any solution yet.

I want to get some data from the users addressbook (for example the single value property kABPersonFirstNameProperty). Because the .takeRetainedValue() function throws an error if this contact doesn't have a firstName value in the addressbook, I need to make sure the ABRecordCopyValue() function does return a value. I tried to check this in a closure:

let contactFirstName: String = {
   if (ABRecordCopyValue(self.contactReference, kABPersonFirstNameProperty) != nil) {
      return ABRecordCopyValue(self.contactReference, kABPersonFirstNameProperty).takeRetainedValue() as String
   } else {
      return ""
   }
}()

contactReference is a variable of type ABRecordRef!

When an addressbook contact provides a firstName value, everything works fine. But if there is no firstName, the application crashes by the .takeRetainedValue() function. It seems, that the if statement doesn't help because the unmanaged return value of the ABRecordCopyValue() function is not nil although there is no firstName.

I hope I was able to make my problem clear. It would be great if anyone could help me out with some brainwave.

Biotin answered 23/9, 2014 at 17:54 Comment(0)
B
32

If I want the values associated with various properties, I use the following syntax:

let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String
let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String

Or you can use optional binding:

if let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String {
    // use `first` here
}
if let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String {
    // use `last` here
}

If you really want to return a non-optional, where missing value is a zero length string, you can use the ?? operator:

let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String ?? ""
let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String ?? ""
Biotin answered 24/9, 2014 at 4:0 Comment(3)
I like your second version. Thank you for this!Biotin
After couple days THIS was the answer I was looking for!! Thank you so much for sharing!Mcwhirter
This still didn't worked for me when accessing records without first/last name when the archive was deployed directly into the phone but finally got it working using NSString instead and using optional binding as shown herePraise
B
5

I got it through this function here:

func rawValueFromABRecordRef<T>(recordRef: ABRecordRef, forProperty property: ABPropertyID) -> T? {
    var returnObject: T? = nil
    if let rawValue: Unmanaged<AnyObject>? = ABRecordCopyValue(recordRef, property) {
        if let unwrappedValue: AnyObject = rawValue?.takeRetainedValue() {
            println("Passed: \(property)")
            returnObject = unwrappedValue as? T
        }
        else {
            println("Failed: \(property)")
        }
    }
    return returnObject
}

You can use it in your property like this:

let contactFirstName: String = {
    if let firstName: String = rawValueFromABRecordRef(recordRef, forProperty: kABPersonFirstNameProperty) {
        return firstName
    }
    else {
        return ""
    }
}()
Bridging answered 23/9, 2014 at 23:54 Comment(3)
Why down vote? I can't correct if you don't say what's wrong?Bridging
Thank you for your answer. Great to see so much help! By the way, i didn't down vote anything. What do you mean?Biotin
@Biotin - I wasn't trying to say it was you, but someone down voted my answer. I'd love to know why since this is what I'm using and it works for me.Bridging
H
1

Maybe it's more than just answering to your question, but this is how I deal with the address book.

I've defined a custom operator:

infix operator >>> { associativity left }
func >>> <T, V> (lhs: T, rhs: T -> V) -> V {
    return rhs(lhs)
}

allowing to chain multiple calls to functions in a more readable way, for instance:

funcA(funcB(param))

becomes

param >>> funcB >>> funcA

Then I use this function to convert an Unmanaged<T> to a swift type:

func extractUnmanaged<T, V>(value: Unmanaged<T>?) -> V? {
    if let value = value {
        var innerValue: T? = value.takeRetainedValue()
        if let innerValue: T = innerValue {
            return innerValue as? V
        }
    }
    return .None
}

and a counterpart working with CFArray:

func extractUnmanaged(value: Unmanaged<CFArray>?) -> [AnyObject]? {
    if let value = value {
        var innerValue: CFArray? = value.takeRetainedValue()
        if let innerValue: CFArray = innerValue {
            return innerValue.__conversion()
        }
    }
    return .None
}

and this is the code to open the address book, retrieve all contacts, and for each one read first name and organization (in the simulator firstName always has a value, whereas department doesn't, so it's good for testing):

let addressBook: ABRecordRef? = ABAddressBookCreateWithOptions(nil, nil) >>> extractUnmanaged
let results = ABAddressBookCopyArrayOfAllPeople(addressBook) >>> extractUnmanaged
if let results = results {
    for result in results {
        let firstName: String? = (result, kABPersonFirstNameProperty) >>> ABRecordCopyValue >>> extractUnmanaged
        let organization: String? = (result, kABPersonOrganizationProperty) >>> ABRecordCopyValue >>> extractUnmanaged
        println("\(firstName) - \(organization)")
    }
}

Note that the println statement prints the optional, so you'll see in the console Optional("David") instead of just David. Of course this is for demonstration only.

The function that answers to your question is extractUnmanaged, which takes an optional unmanaged, unwrap it, retrieves the retained value as optional, unwrap it, and in the end attempts a cast to the target type, which is String for the first name property. Type inferral takes care of figuring out what T and V are: T is the type wrapped in the Unmanaged, V is the return type, which is known because specified when declaring the target variable let firstName: String? = ....

I presume you've already taken care of checking and asking the user for permission to access to the address book.

Humphreys answered 23/9, 2014 at 19:5 Comment(2)
Thank you Antonio for your detailed answer! It's also great to see the flexible and reusable way you deal with it. A slightly modified version of your extractUnmanaged() function (without generics) solved my problem.Biotin
The non generic version was my first draft, then having to convert different types I came up with that generic based solution and all other stuffs. Btw glad it helped youHumphreys

© 2022 - 2024 — McMap. All rights reserved.