如何复制一个带键值的NSDictionary



我有一个在其参数中返回一些值的方法。这些值是有keyvalue的nsdictionary。

我想知道如何使这些nsdictionary的副本,包括键值。

这就是我目前正在做的。

//. h

NSMutableDictionary *tempDic;
@property (strong, nonatomic) NSDictionary *tempDic;

//m

@synthesize tempDic;

- (void)reciverMethod:(NSDictionary *)myDictionary {
// I would like to get my method specific variable **myDictionary** and copy it into my global dictionary value tempDic like so
tempDic = [myDictionary mutableCopy]; //  this dosnt work
}

myDictionary后面可能有几个键值你可以使用

访问
myDictionary.firstvalue
myDictionary.secondvalue
myDictionary.thirdvalue

但是当我尝试使用tempDic时,这些键都不可用。

。tempDic.firstvaluetempDic.secondvaluetempDic。thirdvalue

dosnt工作……

1) remove this, NSMutableDictionary *tempDic;

有这个就够了,

@property (strong, nonatomic) NSDictionary *tempDic;

由于tempDic对象是强的,所以非原子的

- (void)reciverMethod:(NSDictionary)myDictionary {
self.tempDic = myDictionary;
}
编辑1:

id value1 = [self.tempDic objectForKey:@"firstvalue"];
id value2 = [self.tempDic objectForKey:@"secondvalue"];
id value3 = [self.tempDic objectForKey:@"thirdvalue"];

最新更新