NSMutableDictionary with UIButton* as keys - iPhone development

ε祈祈猫儿з 提交于 2019-12-04 03:09:37

One way I found was to use construct an NSValue to use as the key. To create the that use:

[NSValue valueWithNonretainedObject:myButton].

The caveat here seems to be that if the button is garbage collected, the key will hold an invalid reference.

You can get the reference to the UIButton again while looping through the Dictionary like so:

for (NSValue* nsv in myDict) {
    UIButton* b = (UIButton*)[nsv nonretainedObjectValue];
    ...
}

From Apple docs:

The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol).

UIButton does not conform to the NSCopying protocol and so you cannot use it as a key in NSDictionary

AJB

I've got a cool trick for this.

I cast the pointer to an int (since thats all a pointer really is) and store it in an NSNumber. Using the NSNumber as a key solves this problem and makes sense fundementally because who cares about storing a copy of the button in the dictionary? It makes more sense to me to store a copy of the pointer's info.

If your like me, you'll probably wrap that bit up into a macro as well. Something like this:

#define BOX_AS_NUM(_ptr_) [NSNumber numberWithInt:(int)_ptr_]

Then it's a little cleaner to use in code...

NSDictionary* btnMap = [NSDictionary dictionaryWithObjectsAndKeys:some_obj, BOX_AS_NUM(some_btn), nil];
-(IBAction)someBtnAction:(id)sender
{
    SomeObj* obj = [btnMap objectForKey:BOX_AS_NUM(sender)];
    [obj doCoolStuffBecuaseIWasJustClicked];
}

UIButtons have a description property that can be used as a dictionary key:

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithCapacity:1];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10.0f, 10.0f)];
id myObject;
[myDictionary setObject:myObject forKey:myButton.description];

// somewhere else in code
id myLookedUpObject = [myDictionary objectForKey:myButton.description];
// do something with myLookedUpObject
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!