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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:47:28+00:00 2026-06-13T12:47:28+00:00

I saw a very good sample here: Subclass UIButton to add a property What

  • 0

I saw a very good sample here:
Subclass UIButton to add a property

What is it? You can’t add object to a category. But now with this trick you can.

So what is it? How does it work?

Objective-c object already have some constant number of ivar pointers right?

Now you add another one? How did they figure that out?

A pretty ugly notation I must admit.

  • 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-13T12:47:28+00:00Added an answer on June 13, 2026 at 12:47 pm

    With the Associative References trick, you’re not actually adding any instance data to the UIButton object. Instead, you’re using a totally separate Cocoa facility to create a new dictionary mapping (or associating) existing UIButton objects with data that’s stored elsewhere in the heap.

    You could do exactly the same thing without using Cocoa’s Associative References; it would just be even uglier and probably less efficient. It would go something like this, in Objective-C++. (I’m not even going to try to write it in Objective-C, because CFMutableDictionary and NSMutableDictionary both have the wrong behavior on a couple of levels, and I’m not going to write the whole thing from scratch. However, C++’s std::map can’t be used with __weak references the way I want to use it, so I’m falling back on this inefficient std::vector algorithm. For those unfamiliar with C++: std::vector is roughly equivalent to an NSMutableArray, except that you get to choose whether it retains its contents.)

    The point is that the UIButton objects aren’t being changed; what’s changing is the contents of this additional dictionary. The property getter and setter simply know how to look things up in that dictionary so that it appears as if the UIButton has a new property.

    #import "UIButton+Property.h"
    #import <algorithm>
    #import <vector>
    
    typedef std::pair<__weak id, __strong id> EntryType;
    static std::vector<EntryType> myAR;
    
    @implementation UIButton(Property)
    
    -(void) setProperty:(id)property
    {
        for (int i=0; i < myAR.size(); ++i) {
            if (myAR[i].first == self) {
                myAR[i].second = property;
                return;
            }
        }
        myAR.push_back(EntryType(self, property));
    }
    
    -(id) property
    {
        /* To save space, periodically erase the dictionary entries for
         * UIButton objects that have been deallocated. You can skip this
         * part, and even use NSMutableDictionary instead of this C++
         * stuff, if you don't care about leaking memory all over the place.
         */
        size_t n = myAR.size();
        for (size_t i=0; i < n; ++i) {
            if (myAR[i].first == nil)
                myAR[i] = myAR[--n];
        }
        myAR.resize(n);
    
        /* Look up "self" in our dictionary. */
        for (size_t i=0; i < myAR.size(); ++i) {
            EntryType &entry = myAR[i];
            if (entry.first == self) {
                return entry.second;
            }
        }
        return nil;
    }
    
    @end
    

    See also: http://labs.vectorform.com/2011/07/objective-c-associated-objects/

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

Sidebar

Related Questions

I saw this code in http://www.cise.ufl.edu/~manuel/obfuscate/obfuscate.html (http://www.cise.ufl.edu/~manuel/obfuscate/savastio) website.But this code is very very crazy.
This is a very basic question - but apparently google is not very good
I saw some other similar questions on this topic here but they were not
I saw this question , but the answers there are not very relevant. A
It was very strange when I saw this on debugging my application. int iTag
I saw many threads with this tittle, but no one really speak about reuse
I just saw a demo of electric cloud and it was very interesting, but
Hi I'm new to Visual Studio.NET.I saw a tutorial but I can't see any
I am not very good with STL and I saw few post similar to
I am aware that this is a very basic question, but an interviewer asked

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.