IOS5 Developer's Cookbook에 나온 예제..
이 외에도 여러방법이 있다.
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
// | |
// Car.h | |
// Obj-C_Study | |
// | |
// Created by need4spd on 12. 12. 11.. | |
// | |
// | |
#import <Foundation/Foundation.h> | |
@interface Car : NSObject | |
{ | |
//int year; | |
//NSString *make; | |
//NSString *model; | |
} | |
@property (assign) int year; | |
@property NSString *make; | |
@property NSString *model; | |
@property (readonly)NSString *carInfo; | |
- (void) setMake:(NSString *) aMake andModel:(NSString *) aModel andYear:(int) aYear; | |
- (void) printCarInfo; | |
//- (int) year; | |
@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
// | |
// Car.m | |
// Obj-C_Study | |
// | |
// Created by need4spd on 12. 12. 11.. | |
// | |
// | |
#import "Car.h" | |
@implementation Car | |
@synthesize year, make, model, carInfo; | |
- (id) init | |
{ | |
self = [super init]; | |
if (!self) return nil; | |
year = 1901; | |
return self; | |
} | |
- (void) setMake:(NSString *)aMake andModel:(NSString *)aModel andYear:(int)aYear { | |
make = aMake; | |
model = aModel; | |
year = aYear; | |
} | |
- (void) printCarInfo { | |
if(!make) return; | |
if(!model) return; | |
printf("Car Info\n"); | |
printf("Make: %s\n", [make UTF8String]); | |
printf("Model: %s\n", [model UTF8String]); | |
printf("Year: %d\n", year); | |
} | |
- (NSString *) carInfo { | |
return [NSString stringWithFormat: | |
@"Car Info\n Make: %@\n Model: %@\n Yeary: %d", self.make ? self.make : @"Unknown", self.model ? self.model : @"Unknown Model", self.year]; | |
} | |
- (NSMethodSignature *) methodSignatureForSelector:(SEL)aSelector { | |
NSMethodSignature *signature = [super methodSignatureForSelector:@selector(UTF8String)]; | |
if(!signature) { | |
signature = [[self carInfo] methodSignatureForSelector:@selector(UTF8String)]; | |
} | |
return signature; | |
} | |
- (void) forwardInvocation:(NSInvocation *)anInvocation { | |
NSLog(@"in invocation...."); | |
SEL selector = [anInvocation selector]; | |
if([carInfo respondsToSelector:selector]) { | |
printf("[forwarding from %s to %s] ", [[[self class] description] UTF8String], [[NSString description] UTF8String]); | |
[anInvocation invokeWithTarget:self.carInfo]; | |
} | |
} | |
@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
// | |
// RectangleTest.m | |
// Obj-C_Study | |
// | |
// Created by on 12. 6. 22.. | |
// Copyright (c) 2012년 __MyCompanyName__. All rights reserved. | |
// | |
#import "Car.h" | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
//Building And Access Array | |
Car *car = [[Car alloc] init]; | |
NSLog(@"car info %@", [car carInfo]); | |
[car setMake:@"make" andModel:@"model" andYear:1212]; | |
NSLog(@"signature %@ ", [signature description]); | |
//forwarding | |
printf("UTF8String: %s\n", [car performSelector:@selector(UTF8String)]); | |
printf("length: %d\n", [car performSelector:@selector(length)]); | |
//cannot compile code | |
//printf("String length: %d\n", [car length]); | |
} | |
return 0; | |
} |
책에는
signature = [[self carInfo] methodSignatureForSelector:@selector(UTF8String)];
이 부분이
signature = [carInfo methodSignatureForSelector:@selector(UTF8String)];
이렇게 되어있다. 이게 계속 null을 리턴해서, runtime error가 발생하는데 이유를 몰라서보니..
carInfo가 readonly이고, 값이 할당되지 않은 상태라서 null을 리턴하는 것 같아 [self carInfo]로 정상적으로 값이 할당된 NSString을 받아오도록 수정해보았다. 그랬더니 잘 되네...
가장 간단한 forwarding은
forwardingTargetForSelector:(SEL)sel 쓰는거라고 되어있다..
암튼 null이 리턴되는 이유를 찾아내서 (확실하진 않지만..) 다행.. 아오 내 2시간....