@dynamic property needs setter with multiple behaviors
Posted
by ambertch
on Stack Overflow
See other posts from Stack Overflow
or by ambertch
Published on 2010-05-26T06:11:40Z
Indexed on
2010/05/26
6:21 UTC
Read the original article
Hit count: 219
iphone
|objective-c
I have a class that contains multiple user objects and as such has an array of them as an instance variable:
NSMutableArray *users;
The tricky part is setting it. I am deserializing these objects from a server via Objective Resource, and for backend reasons users can only be returned as a long string of UIDs - what I have locally is a separate dictionary of users keyed to UIDs. Given the string uidString of comma separated UIDs I override the default setter and populate the actual user objects:
@dynamic users;
- (void)setUsers:(id)uidString {
users = [NSMutableArray arrayWithArray:
[[User allUsersDictionary] objectsForKeys:[(NSString*)uidString componentsSeparatedByString:@","]]];
}
The problem is this: I now serialize these to database using SQLitePO, which stores these as the array of user objects, not the original string. So when I retrieve it from database the setter mistakenly treats this array of user objects as a string! Where I actually want to adjust the setter's behavior when it gets this object from DB vs. over the network.
I can't just make the getter serialize back into a string without tearing up large code that reference this array of user objects, and I tried to detect in the setter whether I have a string or an array coming in:
if ([uidString respondsToSelector:@selector(addObject)]) {
// Already an array, so don't do anything - just assign users = uidString
but no success... so I'm kind of stuck - any suggestions? Thanks in advance!
© Stack Overflow or respective owner