Last active
August 29, 2015 14:11
-
-
Save matrixfox/65436305a9569f9febee to your computer and use it in GitHub Desktop.
NSCoding
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
NSCoding | |
=== |
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
// | |
// Person.h | |
// NSCoding | |
// | |
// Created by matrixfox on 12/22/14. | |
// Copyright (c) 2014 matrixfox. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface Person : NSObject <NSCoding> | |
#define nameKey @"name" | |
#define ageKey @"age" | |
#define heightKey @"height" | |
#define genderKey @"gender" | |
#define personKey @"person" | |
@property (strong, nonatomic) NSString *name; | |
@property (nonatomic) int age; | |
@property float height; | |
@property char gender; | |
- (id) initWithName:(NSString *)n age:(int)a height:(float)h gender:(char)g; | |
@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
// | |
// Person.m | |
// NSCoding | |
// | |
// Created by matrixfox on 12/22/14. | |
// Copyright (c) 2014 matrixfox. All rights reserved. | |
// | |
#import "Person.h" | |
@implementation Person | |
@synthesize age; | |
@synthesize name; | |
@synthesize height; | |
@synthesize gender; | |
// Person.h custom method | |
- (id) initWithName:(NSString *)n | |
age:(int)a | |
height:(float)h | |
gender:(char)g | |
{ | |
// Call supercalls designated initalizer | |
self = [super init]; | |
// Did the superclass initalizer succeed? | |
if (self) { | |
// Give the instance variables inital values | |
self.name = n; | |
self.age = a; | |
self.height = h; | |
self.gender = g; | |
} | |
// Return the address of the new initalized object | |
return self; | |
} | |
- (id)init | |
{ | |
return [self initWithName:@"" | |
age:0 | |
height:0.0 | |
gender:gender]; | |
} | |
- (NSString *)description | |
{ | |
NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@, %d, %f, %c", | |
self.name, | |
self.age, | |
self.height, | |
self.gender]; | |
return descriptionString; | |
} | |
- (void) dealloc | |
{ | |
NSLog(@"Destoryed: %@", self); | |
} | |
// NSCoding | |
// Unserializing | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super init]; | |
if (self) { | |
name = [aDecoder decodeObjectForKey:nameKey]; | |
age = [aDecoder decodeIntForKey:ageKey]; | |
height = [aDecoder decodeFloatForKey:heightKey]; | |
gender = [aDecoder decodeIntForKey:genderKey]; | |
} | |
return self; | |
} | |
// Serializing | |
- (void) encodeWithCoder:(NSCoder *)aCoder | |
{ | |
[aCoder encodeObject:self.name forKey:nameKey]; | |
[aCoder encodeInt:self.age forKey:ageKey]; | |
[aCoder encodeFloat:self.height forKey:heightKey]; | |
[aCoder encodeInt:self.gender forKey:genderKey]; | |
} | |
@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
// | |
// ViewController.h | |
// NSCoding | |
// | |
// Created by matrixfox on 12/22/14. | |
// Copyright (c) 2014 matrixfox. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@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
// | |
// ViewController.m | |
// NSCoding | |
// | |
// Created by matrixfox on 12/22/14. | |
// Copyright (c) 2014 matrixfox. All rights reserved. | |
// | |
#import "ViewController.h" | |
#import "Person.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
// Building Array | |
NSMutableArray *people = [[NSMutableArray alloc] init]; | |
[people addObject:[[Person alloc] initWithName:@"Jane" age:33 height:5.33 gender:'f']]; | |
[people addObject:[[Person alloc] initWithName:@"Mike" age:28 height:6.2 gender:'m']]; | |
[people addObject:[[Person alloc] initWithName:@"Marry" age:39 height:5.5 gender:'f']]; | |
// NSCoding | |
NSMutableData *data = [[NSMutableData alloc] init]; | |
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; | |
[arch encodeObject:people forKey:personKey]; | |
[arch finishEncoding]; | |
NSMutableArray *otherPeople = [[NSMutableArray alloc] init]; | |
NSKeyedUnarchiver *unarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; | |
otherPeople = [unarch decodeObjectForKey:personKey]; | |
[unarch finishDecoding]; | |
// Testing Loop | |
for (Person *p in otherPeople) { | |
NSLog(@"%@", p); | |
} | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment