gpt4 book ai didi

ios - 读取数组元素的预期方法在 NSDictionary* 类型的对象上找不到

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:31:59 25 4
gpt4 key购买 nike

我知道周围有很多这样的问题,但我认为我的情况有点不同。

    int i = 0;
while (_data[@"VerticalState%i", i] != nil) {
// do things
i++;
}

例如,一个具有 3 个 VerticalState 属性的“级别”将这样实现:VerticalState0、VerticalState1、VerticalState2。

我想使用上面的 while 循环条件读取这些值,它应该在 i = 3 时停止。我怎样才能使上面的代码的想法起作用(显然还有一些其他配置)。仅供引用,_data 是一个 NSDictionary* 实例变量,已经加载了 plist 信息。

最佳答案

您似乎想从字符串格式创建字典键。您需要使用 NSString stringWithFormat:

while (_data[[NSString stringWithFormat:@"VerticalState%i", i]] != nil) {

虽然这样写循环会更好:

int i = 0;
while (1) {
NSString *key = [NSString stringWithFormat:@"VerticalState%i", i];
id value = _dict[key];
if (value) {
// do things
i++;
} else {
break;
}
}

关于ios - 读取数组元素的预期方法在 NSDictionary* 类型的对象上找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043017/

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