nspredicatecore-foundationabaddressbookabrecord

How to user predicates(which requires KVC) to CF object like ABRecord?


I'm devleloping an ABAddressBook based application. I want to use predicates to evaluate or compare the objects in abaddressbook. But it seems like ABAddreesBook is a Core Fundation framework and does not support KVC. Does this mean that I can't use predicates in my application?

Or is there any other way to do things like predicates?


Solution

  • The short answer is: you can't.

    The long answer is: you can, but it'd be a lot of work. I'd probably do something like this:

    1. Recurse through all the NSExpressions of the predicate to aggregate all the -keyPath strings and store them in an array. These will be the properties of the ABRecord objects that you'll be querying.
    2. Iterate through your array of ABRecord objects and create a dictionary to represent each one. This dictionary would have the recordID as well as the ABRecordCopyValue() value of each of the "keypaths" you want, keyed to the property name.
    3. These dictionaries would be aggregated in an array.
    4. This new array (of dictionaries) would be filtered with the predicate
    5. After filtering, the reduced array of dictionaries would converted back into an array of ABRecord objects and returned.

    This sounds like an interesting problem. I'll think about this a bit more and perhaps come back and edit my answer.