我已經子類NSObject。我需要將一個對象(NSMutableArray
實例)設置爲該類的實例的未定義鍵(@"ItemsList"
)。我怎樣才能做到這一點?例如,我想設置,如何在NSObject實例中爲我的自定義鍵(undefinedKey)設置一個值?
[myObject setValue:myArray forUndefinedKey:@"ItemsList"];
我應該怎麼做才能做到這一點?
我已經子類NSObject。我需要將一個對象(NSMutableArray
實例)設置爲該類的實例的未定義鍵(@"ItemsList"
)。我怎樣才能做到這一點?例如,我想設置,如何在NSObject實例中爲我的自定義鍵(undefinedKey)設置一個值?
[myObject setValue:myArray forUndefinedKey:@"ItemsList"];
我應該怎麼做才能做到這一點?
這裏有一個方法可以輕鬆地做到這一點「而無需創建高德」:
@interface MONObject : NSObject
{
NSMutableDictionary * stuff;
}
// ....
- (void)setValue:(id)value forCustomKey:(NSString *)key;
@end
@implementation MONObject
- (void)setValue:(id)value forCustomKey:(NSString *)key
{
assert(self.stuff && value && key);
[self.stuff setValue:value forKey:key];
}
@end
您可以使用這樣一個類,繼承它。 (請參閱下面的子類,並注意 testProperty是如何不被合成,除了被用作動態)
@interface INDynamicPersister : NSObject {
}
/*!
@method saveObject:forKey:
@abstract Invoked when a property setter is invoked.
*/
- (void) saveObject:(id)value forKey:(NSString *)key;
/*!
@method retrieveObjectForKey:
@abstract Invoked when a property getter needs its value.
*/
- (id) retrieveObjectForKey:(NSString *)key;
@end
#define Property_Setter_Suffix @":"
@implementation INDynamicPersister
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
NSMethodSignature *signature = [super methodSignatureForSelector:sel];
if (signature == nil) {
// get the name of the selector
NSString *selectorName = [NSStringFromSelector(sel) lowercaseString];
// if we have a setter property ...
if ([selectorName hasSuffix:Property_Setter_Suffix]) {
signature = [NSMethodSignature signatureWithObjCTypes:"[email protected]:@"];
}
else {
signature = [NSMethodSignature signatureWithObjCTypes:"@@:"];
}
}
return signature;
}
/*!
@method forwardInvocation:
@abstract This method will handle the invocation for the property getters and setters.
*/
- (void)forwardInvocation:(NSInvocation *)anInvocation {
// get the name of the selector
NSString *selectorName = [NSStringFromSelector([anInvocation selector]) lowercaseString];
// if we have a setter property ...
if ([selectorName hasSuffix:Property_Setter_Suffix]) {
int selectorLength = [selectorName length];
NSString *propertyName = [selectorName substringWithRange:NSMakeRange(3, selectorLength - 4)];
// get the value of the invocation
id invocationValue;
[anInvocation getArgument:&invocationValue atIndex:2];
// now set the value for the property
[self saveObject:invocationValue forKey:propertyName];
}
else {
// if we have a getter property ...
id invocationReturnValue = [self retrieveObjectForKey:selectorName];
[anInvocation setReturnValue:&invocationReturnValue];
}
}
- (void) saveObject:(id)value forKey:(NSString *)key {
[NSException raise:@"NotImplementedException" format:@"You need to override this method"];
}
- (id) retrieveObjectForKey:(NSString *)key {
[NSException raise:@"NotImplementedException" format:@"You need to override this method"];
return nil; // compiler is happy now
}
@end
子類可能是與此類似:
@interface MyDynamicEntity : INDynamicPersister {
NSMutableDictionary *propertyValues;
}
@property (retain, nonatomic) NSString * testProperty;
@end
@implementation MyDynamicEntity
@dynamic testProperty;
- (id) init {
if (self = [super init]) {
propertyValues = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void) dealloc {
[propertyValues release], propertyValues = nil;
[super dealloc];
}
- (void) saveObject:(id)value forKey:(NSString *)key {
if (!value) {
[propertyValues removeObjectForKey:key];
}
else {
[propertyValues setObject:value forKey:key];
}
}
- (id) retrieveObjectForKey:(NSString *)key {
return [propertyValues objectForKey:key];
}
@end
希望這有助於:)
@interface MyObject : NSObject
@end
@implementation MyObject {
NSMutableDictionary*itemsList;
}
- (id)valueForUndefinedKey:(NSString*)key {
if ([key isEqualToString:@"itemsList"]) {
return itemsList;
}
else {
return [super valueForUndefinedKey:key];
}
}
- (void)setValue:(id)value forUndefinedKey:(NSString*)key {
if ([key isEqualToString:@"itemsList"]) {
itemsList = value;
}
else {
[super setValue:value forUndefinedKey:key];
}
}
@end
以及調用屬性,有必要通過valueForKey叫它:
MyObject*myObject = [MyObject new];
[myObject setValue:@[@"item1", @"item2"] forKey:@"itemsList"];
NSArray*itemsList = [myObject valueForKey:@"itemsList"];
爲什麼你需要使用未定義的密鑰? – 2011-02-16 09:18:06
我只是想直接設置值而不必創建ivars。 – EmptyStack 2011-02-16 09:20:11