UIView as dictionary key?

后端 未结 3 871
無奈伤痛
無奈伤痛 2021-02-11 20:57

I want to have a NSDictionary that maps from UIViews to something else.

However, since UIViews do not implement the NSCopying pro

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-11 21:46

    Here is the actual code (based on the answer by luvieere and further suggestion by Yar):

    // create dictionary
    NSMutableDictionary* dict = [NSMutableDictionary new];
    // set value
    UIView* view = [UILabel new];
    dict[[NSValue valueWithNonretainedObject:view]] = @"foo";
    // get value
    NSString* foo = dict[[NSValue valueWithNonretainedObject:view]];
    

提交回复
热议问题