//
// main.m
// demo192
//
// Created by SDS107 on 12. 3. 29..
// Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Thingie : NSObject <NSCoding>
{
NSString *name;
int magicNumber;
float shoeSize;
NSMutableArray *subThingies;
}
@property (copy)NSString *name;
@property int magicNumber;
@property float shoeSize;
@property (retain)NSMutableArray *subThingies;
-(id)initWithName:(NSString *)n magicNumber:(int) mn shoeSize:(float)ss;
@end
@implementation Thingie
@synthesize name, magicNumber, shoeSize, subThingies;
-(id)initWithName:(NSString *)n magicNumber:(int)mn shoeSize:(float)ss
{
if(self = [super init]){
self.name = n;
self.magicNumber = mn;
self.shoeSize =ss;
self.subThingies =[NSMutableArray array];
}
return self;
}
-(void)dealloc
{
[name release];
[subThingies release];
[super dealloc];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeInt:magicNumber forKey:@"magicNumber"];
[aCoder encodeFloat:shoeSize forKey:@"shoeSize"];
[aCoder encodeObject:subThingies forKey:@"subThingies"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init]){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];
self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];
self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];
}
return self;
}
-(NSString*)description{
NSString *description =
[NSString stringWithFormat:@"%@: %d/%.1f %@",
name, magicNumber, shoeSize, subThingies ];
return (description);
}
@end
int main(int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
Thingie *thing1;
thing1 = [[Thingie alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10];
NSLog(@"shome thing:%@", thing1);
NSLog(@"직렬화한다.");
NSData *freezeDried;
freezeDried = [NSKeyedArchiver archivedDataWithRootObject:thing1];
[thing1 release];
// 다시 역직렬화 한다.
NSLog(@"역직렬화한다.");
thing1 = [NSKeyedUnarchiver unarchiveObjectWithData:freezeDried];
// 하위객체도 포함한 경우
Thingie *anotherThing;
anotherThing = [[Thingie alloc] initWithName:@"thing2" magicNumber:20 shoeSize:13];
[thing1.subThingies addObject:anotherThing];
NSLog(@"thing with things:%@", thing1);
freezeDried = [NSKeyedArchiver archivedDataWithRootObject:thing1];
thing1 = [NSKeyedUnarchiver unarchiveObjectWithData:freezeDried];
NSLog(@"다시 복구:%@",thing1);
[pool release];
return 0;
}
결과
2012-03-29 11:07:11.249 demo192[3966:403] shome thing:thing1: 42/10.0 (
)
2012-03-29 11:07:11.251 demo192[3966:403] 직렬화한다.
2012-03-29 11:07:11.253 demo192[3966:403] 역직렬화한다.
2012-03-29 11:07:11.253 demo192[3966:403] thing with things:thing1: 42/10.0 (
"thing2: 20/13.0 (\n)"
)
2012-03-29 11:07:11.254 demo192[3966:403] 다시 복구:thing1: 42/10.0 (
"thing2: 20/13.0 (\n)"
)
핵심 메서드 : -valueForKey: 와 setValue:forKey: valueForKeyPath 이다.
//키밸류 코딩 소개
NSString *name = [car1 valueForKey:@"name"];
NSLog(@"%@", name);
NSLog(@"make is %@", [car1 valueForKey:@"make"]);
NSLog(@"모델연식 %@", [car1 valueForKey:@"modelYear"]);
//이번에는 값을 지정해 본다.
[car1 setValue:@"Harold" forKey:@"name"];
[car1 setValue:[NSNumber numberWithFloat:25062.4] forKey:@"mileage"];
NSNumber *avgMileage;
avgMileage = [garage valueForKeyPath: @"cars.@avg.mileage"];
NSLog (@"average is %.2f", [avgMileage floatValue]);
객체의 컬렉션이 어떤 값을 가지는지 검색하고 평가 할 경우 사용.
쓰임새는 마치 SQL문을 쓰는 것 처럼 보인다.
1) predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
2) predicate = [NSPredicate predicateWithFormat:
@"(engine.horsepower > 50) AND (engine.horsepower < 200)"];