Thursday, May 2, 2013

NSKeyedArchiver encodeConditionalObject example ios


encodeConditionalObject :forKey:

Encodes a reference to a given object and associates it with a given key only if it has been unconditionally encoded elsewhere in the archive with encodeObject:forKey:.
- (void)encodeConditionalObject:(id)objv forKey:(NSString *)key
Parameters of [NSKeyedArchiver encodeConditionalObject]
objv
The object to encode.
key
The key with which to associate the encoded value. This value must not be nil.
Example of [NSKeyedArchiver encodeConditionalObject]
 @interface GameBoard : NSObject <NSCoding>
    {
        NSMutableArray *_gamePieces;
    }
 @end

 @interface GamePiece : NSObject <NSCoding>
    {
        GameBoard *_gameBoard; // weak reference to avoid retain cycles
    }
 @end
   - (void)encodeWithCoder: (NSCoder *)coder
    {
        [coder encodeConditionalObject: _gameBoard forKey: @"gameBoard"];
    }