Ios 如何在字典中传递常数
我想知道是否有办法将UITableViewStylePlain这样的常量传递到NSDictionary中 我使用NSValue传递值类型,如下所示:Ios 如何在字典中传递常数,ios,objective-c,nsdictionary,nsvalue,Ios,Objective C,Nsdictionary,Nsvalue,我想知道是否有办法将UITableViewStylePlain这样的常量传递到NSDictionary中 我使用NSValue传递值类型,如下所示: CGRect myRec = CGRectMake(0,0,320,568); NSDictionary *myDic = @{@"val":[NSValue valueWithCGRect:myRec]}; CGRect yourRec = [[myDic valueForKey:@"val"] CGRectValue]; 我的问题是,对于像U
CGRect myRec = CGRectMake(0,0,320,568);
NSDictionary *myDic = @{@"val":[NSValue valueWithCGRect:myRec]};
CGRect yourRec = [[myDic valueForKey:@"val"] CGRectValue];
我的问题是,对于像UITableViewStylePlain这样的常量,我如何做到这一点?大概是这样的:
NSDictionary *myDic = @{@"style":[NSValue value:UITableViewStylePlain withObjCType:enum]};
我意识到enum不是Objective类型…:(枚举值是数字,因此您可以使用
NSNumber
将它们存储在字典中:
NSDictionary *myDic = @{@"style":@[NSNumber numberWithInt:UITableViewStylePlain]};
enum
通常保存NSInteger
(有时NSInteger
),因此在这种情况下NSNumber
也足够好,例如:
NSDictionary * myDic = @{ @"style" : @(UITableViewStylePlain) };