Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you call a property setter by name?

Tags:

objective-c

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 invocationWithMethodSignature:[[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);
like image 459
jtalarico Avatar asked Dec 29 '10 00:12

jtalarico


People also ask

What is a property getter?

Property getters and setters allow you to change the default behavior when accessing or modifying object properties.

What is a setter in JS?

In JavaScript, a setter can be used to execute a function whenever a specified property is attempted to be changed. Setters are most often used in conjunction with getters to create a type of pseudo-property. It is not possible to simultaneously have a setter on a property that holds an actual value.

What will happen if you bind to a property for which no public setter is available?

If no getter or setter for the field exists, a direct access for the field is performed. If a getter exists for the field, it will be executed when trying to read the field from outside.

What are setters and getters in JavaScript?

Getter — binds an object property to a function that will be called when that property is looked up. Setter — binds an object property to a function to be called when there is an attempt to set that property. It gives a simpler syntax for the properties and methods of an object.


1 Answers

Use Key-Value Coding.

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

like image 80
bbum Avatar answered Oct 14 '22 11:10

bbum