Tuesday, February 12, 2013

Categories vs SubClass

Prefer Categories for adding functionality to an existing class and also want to affect the behaviour of All instances
whereas SubClassing is better option for customization and if benaviour of only certain instance needs to change and retain the original method for others.

Wednesday, February 6, 2013

NSUserDefaults-Mutable method sent to immutable object

As the documentation for NSUserDefaults says: "Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value." Whenever you want to change a collection you get from NSUserDefaults you have to get the imutable version, make a mutableCopy, modify that, and set it back again.

For E.g:


   NSMutableArray *placesT= nil;
   NSrray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"];

if (tempArray) {
    placeT = [tempArray mutableCopy];
} else {
    placesT=[[NSMutableArray alloc] init];
}

[placesT addObject: [NSString stringWithFormat:@"%@", tagF.text] ];

NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:@"placesT"];
[tUD synchronize];