简体   繁体   中英

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

Here is how to do so:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM