gpt4 book ai didi

ios - 使用 NSDictionary 作为其他 NSDictionary 的键

转载 作者:行者123 更新时间:2023-11-28 19:01:13 24 4
gpt4 key购买 nike

我正在尝试实现以下结构:

NSMutableDictionary *dict = [@{} mutableCopy];

NSDictionary *key1 = @{@"id_format": @(1), @"date": @"2014-08-01"};
NSDictionary *key2 = @{@"id_format": @(2), @"date": @"2014-08-02"};

// This runs perfect and can be checked in llvm debugger
// data1 & data2 are NSArray that contain several NSDictionary
[dict setObject:data1 forKey:key1];
[dict setObject:data2 forKey:key2];

// Later, if i try to access dict using another key, returns empty NSArray
NSDictionary *testKey = @{@"id_format": @(1), @"date": @"2014-08-01"}; // Note it's equal to "key1"

for(NSDictionary *dictData in dict[testKey]){
// dictData is empty NSArray
}

// OR

for(NSDictionary *dictData in [dict objectForKey:testKey]){
// dictData is empty NSArray
}

所以问题是是否可以使用 NSDictionary 作为键。

最佳答案

一个对象如果符合NSCopying就可以作为键,并且应该实现hashisEqual:来比较值而不是而不是身份。

字典遵循为 hash 返回 [self count] 的数组约定。所以这是一个非常糟糕的散列,但它在技术上是有效的。这意味着您的外部字典最终将执行有效的线性搜索,但它会起作用。

字典实现并正确响应 isEqual:。他们还实现了 NSCopying

因此您可以使用字典作为字典键。

关于ios - 使用 NSDictionary 作为其他 NSDictionary 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com