Friday, May 3, 2013

NSKeyedUnarchiver setClass forClassName example ios

[NSKeyedUnarchiver setClass forClassName]
Adds a class translation mapping to NSKeyedUnarchiver whereby objects encoded with a given class name are decoded as instances of a given class instead.
+ (void)setClass:(Class)cls forClassName:(NSString *)codedName
Parameters of [NSKeyedUnarchiver setClass forClassName]
cls
The class with which to replace instances of the class named codedName.
codedName
The ostensible name of a class in an archive.
Discussion of [NSKeyedUnarchiver setClass forClassName]
When decoding, the class’s translation mapping is used only if no translation is found first in an instance’s separate translation map.
Example of [NSKeyedUnarchiver setClass forClassName]
 MyViewController *controller = [[[MyViewController alloc] init] autorelease];
   NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:[NSKeyedArchiver archivedDataWithRootObject:controller]] autorelease];
   [unarchiver setClass:[MyNavigationBar class] forClassName:@"UINavigationBar"];
   controller = [unarchiver decodeObjectForKey:@"root"];
Example of [NSKeyedUnarchiver setClass forClassName]
UIViewController *tempController = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:tempController] autorelease];

NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:navigationController];
NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:archive] autorelease];
[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];
UINavigationController *customNavigationController = [unarchiver decodeObjectForKey:@"root"];

UIViewController *contentController = [[[ContentViewController alloc] init] autorelease];
customNavigationController.viewControllers = [NSArray arrayWithObject:contentController];
End of article on [NSKeyedUnarchiver setClass forClassName]