Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:52:36+00:00 2026-06-17T12:52:36+00:00

I have an object which has a property which is a struct like the

  • 0

I have an object which has a property which is a struct like the following:

struct someStruct{
    float32 x, y;
};

And what I’d like to do is invoke the getter for that struct property via a string:

id returnValue = [theObject performSelector:NSSelectorFromString(@"thePropertyName")];

But as you can see “performSelector:” returns an object, not a struct. I’ve tried every way of casting I could think of, to no avail, which makes me think I’m missing something – perhaps something easy…

Any ideas how returnValue can be coaxed back into a struct? Thanks!

Edit:
Whoever the original responder was (he’s since deleted his post for some reason) – you were right: The following, based on your answer, works:

StructType s = ((StructType(*)(id, SEL, NSString*))objc_msgSend_stret)(theObject, NSSelectorFromString(@"thePropertyName"), nil);

Edit 2: A fairly detailed look at the issue can be found here.

Edit 3: For symmetry’s sake, here’s how to set a struct property by its string name (note that this is exactly how the accepted answer accomplishes setting, whereas my problem required the slightly different approach for the getter mentioned in the first edit above):

NSValue* thisVal = [NSValue valueWithBytes: &thisStruct objCType: @encode(struct StructType)];
[theObject setValue:thisVal forKey:@"thePropertyName"];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:52:37+00:00Added an answer on June 17, 2026 at 12:52 pm

    You can do this using Key Value Coding by wrapping the struct inside an NSValue (and unwrapping it when it is returned). Consider a simple class with a struct property, as shown below:

    typedef struct {
        int x, y;
    } TwoInts;
    
    @interface MyClass : NSObject
    
    @property (nonatomic) TwoInts twoInts;
    
    @end
    

    We can then wrap and unwrap the struct in an NSValue instance to pass it to and from the KVC methods. Here is an example of setting the value of the struct using KVC:

    TwoInts twoInts;
    twoInts.x = 1;
    twoInts.y = 2;
    NSValue *twoIntsValue = [NSValue valueWithBytes:&twoInts objCType:@encode(TwoInts)];
    MyClass *myObject = [MyClass new];
    [myObject setValue:twoIntsValue forKey:@"twoInts"];
    

    To get the struct as a return value, use NSValue‘s getValue: method:

    TwoInts returned;
    NSValue *returnedValue = [myObject valueForKey:@"twoInts"];
    [returnedValue getValue:&returned];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object which has a property that contains an IEnumerable of objects.
I have an object which has several properties that are set when the object
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have an Object CaseNote which has numerous child object's like CaseNoteContactType . For
I have a Task object which has a property of Employee. Im trying to
I have an object which has a huge number of properties. I'd like to
I have an object named game which has a property called channels (which is
I have an 3 object hierarchy which has a root as 'Division' that has
i have x which an object of type objectX which has a property ListOfObjectYs
I have an object which has status property. But I can't get the value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.