Get reference to the NSObject in an NSValue

640 Views Asked by At

I have an NSMutableDictionary which contains UIImageView objects and UIView keys.

When I am setting an UIImageView to the NSMutableDictionary I get the NSValue out from the UIView like this:

[viewImageMap setObject:anImage forKey:[NSValue valueWithNonretainedObject:aView]];

I want to know if there is anyway I can obtain the UIView reference back with the NSValue object. Something like this:

UIView *aView = (UIView *)[NSObject objectWithValue:aValue];
2

There are 2 best solutions below

0
jscs On BEST ANSWER

For each valueWith<Type>: method, NSValue has a complementary method that returns the stored value in the appropriate type.

The one you need is nonRetainedObjectValue.

UIView * theView = [theValue nonRetainedObjectValue];
12
Julien Quere On

Here is how to do so:

NSValue * myViewKey = [NSValue valueWithNonretainedObject:myView];
UIView * myViewFromValue = (UIView*) myViewKey.nonretainedObjectValue