开发者

How do you call a property setter by name?

I've asked a related question, but thought I'd split this out into its own question. See the code below for calling a property getter.

SEL propSelector = NSSelectorFromString(propertyName);
NSInvocation *inv = [NSInvocation invocationWithMeth开发者_如何学JAVAodSignature:[[target class]instanceMethodSignatureForSelector:propSelector]];
[inv setSelector:propSelector];
[inv setTarget:target];
[inv invoke];
float value;
[inv getReturnValue:&value];

I'd like to do the same thing, but call the property SETTER. I'd also like to avoid manually crafting the setter name by building a @"setPropertyName:" string. Bottom line - is it possible to use the selector created on this line to call the setter?

SEL propSelector = NSSelectorFromString(propertyName);


Use Key-Value Coding.

I.e. [someObject setValue: anObjectValue forKey: @"foo"];


Having read your other question earlier today, this is what I can come up with:

It's not possible to use it directly as the setter. The getter does not accept any arguments, so your selector will never have the colon, so it's never gonna work.

Easiest thing to do is define your setter methods as foo: instead of setFoo:. This way you'll only have to append the colon (which is quite cheap, requires only two extra strings)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜