objective-ccocoacocoa-touchnsvalue

Get reference to the NSObject in an NSValue


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];

Solution

  • 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];