I know the string name of a property of an object. How would I go about getting and setting that property using the string?
我知道对象属性的字符串名称。我将如何使用字符串获取和设置该属性?
2 个解决方案
#1
48
While @weichsel is correct, there is a better way.
虽然@weichsel是正确的,但还有更好的方法。
Use:
使用:
[anObject valueForKey: @"propertyName"];
and
和
[anObject setValue:value forKey:@"propertyName"];
Obviously, @"propertyName"
can be an NSString
that is dynamically composed at runtime.
显然,@“propertyName”可以是在运行时动态组合的NSString。
This technique is called Key Value Coding and is fundamental to Cocoa.
这种技术称为键值编码,是Cocoa的基础。
Why this is better is because -valueForKey
will do what is necessary to "box" whatever type the property returns into an object. Thus, if the property is of type int
, it'll return an NSNumber
instance containing the int.
为什么这样做更好是因为-valueForKey将执行“绑定”属性返回到对象的任何类型所需的操作。因此,如果属性是int类型,它将返回包含int的NSNumber实例。
This is much easier to deal with -- performSelector
will only work for types that happen to fit into a pointer's worth of memory.
这更容易处理 - performSelector仅适用于恰好适合指针内存的类型。
Note that there is also -setValue:forKey:
.
请注意,还有-setValue:forKey:。
#2
2
@synthesize propertyName
automates the generation of getter and setter methods.
@synthesize propertyName自动生成getter和setter方法。
The compiler generates
编译器生成
- (id)propertyName;
- - (id)propertyName;
- (void)setPropertyName;
- - (void)setPropertyName;
If you have a selector as NSString, you can use performSelector:NSSelectorFromString
.
e.g.:[object performSelector:NSSelectorFromString(@"propertyName") ...]
如果您有一个选择器作为NSString,您可以使用performSelector:NSSelectorFromString。例如:[object performSelector:NSSelectorFromString(@“propertyName”)...]
#1
48
While @weichsel is correct, there is a better way.
虽然@weichsel是正确的,但还有更好的方法。
Use:
使用:
[anObject valueForKey: @"propertyName"];
and
和
[anObject setValue:value forKey:@"propertyName"];
Obviously, @"propertyName"
can be an NSString
that is dynamically composed at runtime.
显然,@“propertyName”可以是在运行时动态组合的NSString。
This technique is called Key Value Coding and is fundamental to Cocoa.
这种技术称为键值编码,是Cocoa的基础。
Why this is better is because -valueForKey
will do what is necessary to "box" whatever type the property returns into an object. Thus, if the property is of type int
, it'll return an NSNumber
instance containing the int.
为什么这样做更好是因为-valueForKey将执行“绑定”属性返回到对象的任何类型所需的操作。因此,如果属性是int类型,它将返回包含int的NSNumber实例。
This is much easier to deal with -- performSelector
will only work for types that happen to fit into a pointer's worth of memory.
这更容易处理 - performSelector仅适用于恰好适合指针内存的类型。
Note that there is also -setValue:forKey:
.
请注意,还有-setValue:forKey:。
#2
2
@synthesize propertyName
automates the generation of getter and setter methods.
@synthesize propertyName自动生成getter和setter方法。
The compiler generates
编译器生成
- (id)propertyName;
- - (id)propertyName;
- (void)setPropertyName;
- - (void)setPropertyName;
If you have a selector as NSString, you can use performSelector:NSSelectorFromString
.
e.g.:[object performSelector:NSSelectorFromString(@"propertyName") ...]
如果您有一个选择器作为NSString,您可以使用performSelector:NSSelectorFromString。例如:[object performSelector:NSSelectorFromString(@“propertyName”)...]