gpt4 book ai didi

ios - Obj-C setValuesForKeysWithDictionary 64 位与 32 位

转载 作者:行者123 更新时间:2023-11-28 21:51:27 25 4
gpt4 key购买 nike

我正在从我的 API 中提取一个 JSON 对象并使用以下代码创建它(事后看来不是正确的方法):

+ (YActivity *)instanceFromDictionary:(NSDictionary *)jsonDictionary
{
YActivity * instance = [[YActivity alloc] init];
[instance setAttributesFromDictionary:jsonDictionary];
return instance;
}

- (void)setAttributesFromDictionary:(NSDictionary *)jsonDictionary
{
if (![jsonDictionary isKindOfClass:[NSDictionary class]]) {
return;
}

[self setValuesForKeysWithDictionary:jsonDictionary];
}

其中一个键是“类型”。我有一个名为“type”的只读变量@synthesized。在我的应用程序的 32 位版本上,这是在调用 setValue:(id)value forUndefinedKey:(NSString *)key 之前立即设置的。我在那个方法中引用了这个值,在我的应用程序的 64 位版本上,当断点到达这个方法时,类型还没有设置。

显然这不是最好的做法。我只是想知道是否有其他人看到了这个,或者我是否在咆哮错误的树。我比较了两个版本之间的文件,它们是相同的。我在 iOS 8.1 模拟器上运行它们,API 为两者返回相同的东西......我很难过。基本上在旧版本上 defined 键设置在 undefined 之前,而在新版本上似乎与此相反。

最佳答案

NSDictionary 对象是无序集合,因此代码不应该假设字典枚举其自己的键的顺序。事实证明,32 位和 64 位运行时之间的实现差异会影响散列值最终存储的位置。

由于 API 契约(Contract)明确不保证顺序,这不应该导致问题,但它可能(在这种情况下显然确实)产生了导致代码的副作用,以前 '为 64 位架构编译时会中断。

在不显着改变实现的情况下解决您当前遇到的问题的一种快速方法是自己枚举字典的键,这将允许您提供一个按您希望的方式排序的键数组:

- (void)setAttributesFromDictionary:(NSDictionary *)dictionary
{
// So instead of doing this...
//
// [self setValuesForKeysWithDictionary:dictionary];

// You could do something along these lines:
//
NSMutableArray *keys = dictionary.allKeys.mutableCopy;

// TODO: insert code to change the order of the keys array.
// Then loop through the keys yourself...

for (NSString *key in keys)
{
[self setValue:dictionary[key] forKey:key];
}
}

关于ios - Obj-C setValuesForKeysWithDictionary 64 位与 32 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28178761/

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