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 8484907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:29:55+00:00 2026-06-10T20:29:55+00:00

Does anyone know how to set struct variables using object_setInstanceVariable? It is work fine

  • 0

Does anyone know how to set struct variables using object_setInstanceVariable? It is work fine for reference types:

object_setInstanceVariable(obj, "_variable", ref);

But do not work for structs such as CGPoint. Tried following ways:

1. object_setInstanceVariable(obj, "_variable", point);
2. object_setInstanceVariable(obj, "_variable", &point);
3. object_setInstanceVariable(obj, "_variable", (void **)&point);

I’m not sure I understand what I’m doing (especially for the last step)…

Thanks You in advance!!!

  • 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-10T20:29:57+00:00Added an answer on June 10, 2026 at 8:29 pm

    object_setInstanceVariable and object_getInstanceVariable are really supposed to be used for objects only (though the interface declaration seems to indicate otherwise). You should not use those for builtins.

    The more appropriate approach is to use class_getInstanceVariable and ivar_getOffset.

    For example…

    static void* getIvarPointer(id object, char const *name) {
        Ivar ivar = class_getInstanceVariable(object_getClass(object), name);
        if (!ivar) return 0;
        return (uint8_t*)(__bridge void*)object + ivar_getOffset(ivar);
    }
    

    That cast up there basically turns the object pointer into a pointer to a byte, so it can be added to the offset (which gives us the pointer to the iVar). We have to go through a void* first, because ARC does not allow a direct cast to uint8_t*.

    It’s use can be demonstrated thusly…

    @implementation Foo {
        CGRect _rect;
    }
    
    - (CGRect*)rectPointer {
        CGRect *ptr = getIvarPointer(self, "_rect");
        NSAssert(ptr == &_rect, @"runtime discovered pointer should be same as iVar pointer");
        return ptr;
    }
    

    Now, you have a way to get the pointer of any iVar… just cast the returned void* appropriately.

    BTW, I don’t recommend doing this in general, but like any tool, there are uses for the runtime functions.

    Oops. Forgot to show setting the value…

    CGRect *rectPtr = getIvarPointer(someObject, "_rect");
    if (rectPtr) {
        *rectPtr = CGRectMake(1, 2, 3, 4);
    } else {
        // Handle error that the ivar does not exist...
    }
    

    EDIT

    I’m sorry, it’s still not really clear. Should I get this type
    (CGRect*) and then pass it to object_setInstanceVariable? Also, I have
    an error “Undefined symbols “_getIvarPointer”” – please let me know
    what H file should I include – Dmitry

    No. You should not use object_setInstanceVariable at all. It’s supposed to be used only for instance variables that are objects. Yours is a CGRect, so you should not use that function at all.

    Use it like the very last example. Maybe this one is a bit more clear (it takes from your example).

    Instead of doing something like your question asked:

    object_setInstanceVariable(obj, "_variable", point);
    

    Do this…

    // Get a pointer to the instance variable.  Returns NULL if not found.
    CGPoint *pointPtr = getIvarPointer(obj, "_variable");
    if (pointPtr) {
        // Now, assign to the point, by de-referencing the pointer.
        *pointPtr = point;
    }
    

    Note, that I defined the getIvarPointer function as static which has file-local scope. It is not an apple-supplied function – you have to write it. If you are going to use this in multiple places, you need to remove the static so the linker can find it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how to set the RepeatColumns property of an ASP.Net Datalist using
Does anyone know how to set up auto completion to work nicely with python,
Does anyone know how to sort a list of struct with different types (sample
Does anyone know how to set the day of the week when a date
Does anyone know how to set the TextBox.Width property to fill up the remainder
Does anyone know how to set the initial folder of the open project or
Does anyone know how to set the working directory in JavaScript before? Code I
Does anyone know how to set Visual Studio 2008 to output the current date/time
Does anyone know how to set the background colour of just a section of
Does anyone know a link to a set of color profiles for Eclipse that

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.