gpt4 book ai didi

iphone - 如何在 NSObject 实例中为我的自定义键 (undefinedKey) 设置一个值?

转载 作者:搜寻专家 更新时间:2023-10-30 20:04:11 24 4
gpt4 key购买 nike

我已经子类化了 NSObject。我需要将一个对象(NSMutableArray 实例)设置为该类实例的未定义​​键(@"ItemsList")。我怎样才能做到这一点?例如,我想设置,

[myObject setValue:myArray forUndefinedKey:@"ItemsList"];

我应该做些什么来完成这项工作?

最佳答案

这是一种“无需创建 ivars”即可轻松完成此操作的方法:

@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

关于iphone - 如何在 NSObject 实例中为我的自定义键 (undefinedKey) 设置一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5014448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com