Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created November 21, 2012 15:13
Show Gist options
  • Select an option

  • Save codeswimmer/4125340 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/4125340 to your computer and use it in GitHub Desktop.
iOS: NSKeyedArchiver example
@interface ColorTile : NSObject <NSCoding> {
CGPoint tileOrigin;
UIColor *tileColor;
}
@property(nonatomic) CGPoint tileOrigin;
@property(nonatomic, retain) UIColor *tileColor;
@end
#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
- (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