Created
November 21, 2012 15:13
-
-
Save codeswimmer/4125340 to your computer and use it in GitHub Desktop.
iOS: NSKeyedArchiver example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface ColorTile : NSObject <NSCoding> { | |
| CGPoint tileOrigin; | |
| UIColor *tileColor; | |
| } | |
| @property(nonatomic) CGPoint tileOrigin; | |
| @property(nonatomic, retain) UIColor *tileColor; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ColorTile.h" | |
| @implementation ColorTile | |
| @synthesize tileColor, tileOrigin; | |
| - (void)encodeWithCoder:(NSCoder *)coder { | |
| [coder encodeObject:self.tileColor forKey:@"TileColor"]; | |
| [coder encodeFloat:self.tileOrigin.x forKey:@"TileOriginX"]; | |
| [coder encodeFloat:self.tileOrigin.y forKey:@"TileOriginY"]; | |
| } | |
| - (id)initWithCoder:(NSCoder *)coder { | |
| self = [super init]; | |
| if (self != nil) { | |
| self.tileColor = [coder decodeObjectForKey:@"TileColor"]; | |
| float tx = [coder decodeFloatForKey:@"TileOriginX"]; | |
| float ty = [coder decodeFloatForKey:@"TileOriginY"]; | |
| self.tileOrigin = CGPointMake(tx, ty); | |
| } | |
| return self; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void)copy:(id)sender { | |
| // Get the General pasteboard and the current tile. | |
| UIPasteboard *gpBoard = [UIPasteboard generalPasteboard]; | |
| ColorTile *theTile = [self colorTileForOrigin:currentSelection]; | |
| if (theTile) { | |
| // Create an archive of the ColorTile object as an NSData object. | |
| NSData *tileData = [NSKeyedArchiver archivedDataWithRootObject:theTile]; | |
| if (tileData) | |
| // Write the archived data to the pasteboard. | |
| [gpBoard setData:tileData forPasteboardType:ColorTileUTI]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment