Yes. You can either use key-value coding: [[self valueForKey:myString] release];

or you can use the ObjC runtime functions to do exactly that.

#import <objc/runtime.h>

id value = nil;
object_getInstanceVariable(self, [myStr UTF8String], &value); // if your ivar 
is auto synthesized from a property, you’d need to prepend an underscore. 
[value release];


No need to do that when you have Key-Value Coding, however. Just use that. 


On Jun 18, 2014, at 11:23 AM, Trygve Inda <cocoa...@xericdesign.com> wrote:

> If I have a class:
> 
> @interface MyClass : NSObject
> {
>    NSNumber*    myNumber;
> }
> @property (nonatomic, retain) NSNumber* myNumber;
> 
> 
> And in the class implementation I have:
> 
> -(void)doSomething
> {
>    // it could get this string from anywhere, not always a constant
>    NSString* myString = @"myNumber";
> 
>     ???
> }
> 
> 
> Is there a way to access the myNumber property and send it a message knowing
> only it's name (contained in myString)?
> 
> So rather than doing:
> 
> [myNumber release];
> 
> I could do something like:
> 
> [getProperty(myString) release];
> 
> 
> Thanks,
> 
> Trygve
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/danhd123%40mac.com
> 
> This email sent to danhd...@mac.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to