NSKeyedArchiver category

Each method in the NSKeyedArchiver category corresponds to a method in the NSKeyedArchiver class, but provides a secure version of the functionality. For more information about the functionality and usage, see NSKeyedArchiver in developer.apple.com.

The category methods return an NSError object. The methods set the properties on the object as described in NSError objects that secure Objective-C methods return.

The following table shows each added method and its corresponding method in NSKeyedArchiver.

Table 26.   NSKeyedArchiver category methods

Method in category

Corresponding method in NSKeyedArchiver

+ (BOOL)archiveRootObject:

(id)rootObject

toSecureFile:(NSString *)path

error:(

NSError *__autoreleasing *)error;

+ (BOOL)archiveRootObject:

(id)rootObject

toFile:(NSString *)path;

+ (BOOL)archiveRootObject:

(id)rootObject

toSecureFile:(NSString *)path

atomically:(BOOL)atomically

error:(

NSError *__autoreleasing *)error;

+ (BOOL)archiveRootObject:

(id)rootObject

toFile:(NSString *)path

atomically:(BOOL)atomically;

Example:

The following example shows how to use NSKeyedArchiver and NSKeyedUnarchiver category methods to:

1. Create a secure archive file and write data to it from a mutable dictionary.
2. Read the contents of the secure archive file into another mutable dictionary.

For brevity, the example does not include error handling.

- (void)NSKeyedArchiverCategoryExample
{
	NSError *error;
 
	// Create and populate a mutable dictionary. 
	NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 
	NSString *key1 = @"baseball";
	NSString *value1 = @"white";
	[dict setValue:value1 forKey:key1];
 
	NSString *key2 = @"basketball";
	NSString *value2 = @"orange";
	[dict setValue:value2 forKey:key2];
 
	// Archive the dictionary to a secure file.
	NSString *archiveName = @"/tmp/secureArchive";
		    [NSKeyedArchiver archiveRootObject:dict toSecureFile:archiveName error:&error];
 
	// Unarchive the secure file contents into another dictionary. 
	NSMutableDictionary *dictCopy = (NSMutableDictionary*)[NSKeyedUnarchiver 
					unarchiveObjectWithSecureFile:archiveName error:&error];
 
	// Note: The contents of NSMutableDictionary objects 'dict' and 'dictCopy' 
	// are identical.
 
}