Cocoa Dev
Cocoa Dev

Reputation: 9541

NSMutableArray of ABRecordRef. How do I serialize?

I have an NSMutableArray with ABRecordRef

I want to save it to Disk and my co-worker told me to Serialize it. I have no idea what he's talking about?

I've surfed the web and I 've seen so many "solutions" but I don't understand how it works or why it's needed

Upvotes: 1

Views: 1028

Answers (1)

Jeethu
Jeethu

Reputation: 429

ABRecord is an opaque Core Foundation class without a corresponding toll-free bridged objective-c class. If it were an Objective-C class, you could've just used NSCoder as @Toro suggests.

Assuming your mutable array is a list of ABPerson references, you can do something like this:

NSMutableArray newArr = [NSMutableArray array];
for(id obj in mutableArray) {
    ABRecordRef ref = (ABRecordRef)obj;
    ABRecordID recordID = ABRecordGetRecordID(ref);
    NSValue* v = [NSValue value:recordID withObjCType:@encode(ABRecordID)];
    [newArr addObject:v];
}
// newArr is a list of NSValues holding ABRecordIDs, which can be serialized.

While unserializing, get the ABRecordID from each NSValue and then call ABAddressBookGetPersonWithRecordID() on it to get the ABRecordRef back. Just remember that it's possible that the record might be deleted by the user and as a result, you might get a NULL value back from this function.

Upvotes: 2

Related Questions