Core Data object into an NSDictionary with possible nil objects

Posted by Chuck on Stack Overflow See other posts from Stack Overflow or by Chuck
Published on 2011-01-02T03:47:29Z Indexed on 2011/01/02 3:54 UTC
Read the original article Hit count: 180

Filed under:
|
|

I have a core data object that has a bunch of optional values. I'm pushing a table view controller and passing it a reference to the object so I can display its contents in a table view. Because I want the table view displayed a specific way, I am storing the values from the core data object into an array of dictionaries then using the array to populate the table view. This works great, and I got editing and saving working properly. (i'm not using a fetched results controller because I don't have anything to sort on)

The issue with my current code is that if one of the items in the object is missing, then I end up trying to put nil into the dictionary, which won't work.

I'm looking for a clean way to handle this, I could do the following, but I can't help but feeling like there's a better way.

*passedEntry is the core data object handed to the view controller when it is pushed, lets say it contains firstName, lastName, and age, all optional.

if ([passedEntry firstName] != nil) {
    [dictionary setObject:[passedEntry firstName] forKey:@"firstName"]
}
else {
    [dictionary setObject:@"" forKey:@"firstName"]
}

And so on. This works, but it feels kludgy, especially if I end up adding more items to the core data object down the road.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c