Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 搜索字典可以使用硬编码变量查找,但在其他情况下会崩溃_Ios_Objective C_Nsarray - Fatal编程技术网

Ios 搜索字典可以使用硬编码变量查找,但在其他情况下会崩溃

Ios 搜索字典可以使用硬编码变量查找,但在其他情况下会崩溃,ios,objective-c,nsarray,Ios,Objective C,Nsarray,我有以下代码来搜索以下词典: //NSString *knownObject = @"3:40 am"; NSArray *temp = [itemDict allKeysForObject:knownObject]; NSString *key = [temp objectAtIndex:0]; NSLog(@"prayer: %@", key); 格言: 运行第一行时,它会正确返回“shurooq”。但是,当我使用变量时: NSArray *temp = [itemDict all

我有以下代码来搜索以下词典:

//NSString *knownObject = @"3:40 am";
NSArray *temp = [itemDict allKeysForObject:knownObject];
NSString *key = [temp objectAtIndex:0];
NSLog(@"prayer: %@", key);
格言:

运行第一行时,它会正确返回“shurooq”。但是,当我使用变量时:

    NSArray *temp = [itemDict allKeysForObject:nextPrayerTime];
nextrayertime
的日志输出与预期一样,只是
3:40 am

为什么这不起作用


非常感谢

下一个播放时间的值是多少

根据文档,
allKeysForObject:
返回

一个新数组,包含与字典中某个对象的所有匹配项相对应的键。如果找不到与某个对象匹配的对象,则返回空数组


根据给出的信息,我猜您正在返回一个空数组,当您调用
[temp objectAtIndex:0]
时,您正在访问一个不存在的索引,并得到一个越界异常。

我对我的答案不是100%肯定,但是,当对象是NSString时,isEqualToString方法是否用于比较对象


否则,您可能需要与字典中的原始对象进行比较(使用相同的NSString)。

检查
NextPayerTime
的值,如果
itemDict
包含与
NextPayerTime
相同的任何对象,则
[itemDict allKeysForObject:NextPayerTime]
将返回一个键数组,否则将返回空数组

在您当前的情况下,无论如何都会得到一个空数组,但当您尝试访问
[temp objectAtIndex:0]
时,编译器找不到任何对象,它会得到一个超出绑定的数组异常,这会导致应用程序崩溃

要克服此异常,您应该检查数组中对象的计数

NSArray *temp = [itemDict allKeysForObject: nextPrayerTime];
if([temp count] > 0) {
    NSString *key = [temp objectAtIndex:0];
    NSLog(@"prayer: %@", key);
} else
    NSLog(@"NO Object Found");

你到底想要什么?在标题中用fine替换find。当您使用“NextPayerTime”作为allKeysForObject的参数时,还要确认您的代码正在崩溃。你能展示一下你用来创建NextPayerTime的代码吗?它可能在这一行[temp objectAtIndex:0]崩溃了。发布你的完整代码,包括NextPayerTime中的内容。最有可能的是,“NextPayerTime”与dict中的键不完全相同。“temp”返回时没有任何条目,因此objectAtIndex在上面拉屎。提示,do
NSLog(@“它们是%s”,([NextPayerTime isEqualToString:KnowNoObject]?“相等”:“不相等”);
NSArray *temp = [itemDict allKeysForObject: nextPrayerTime];
if([temp count] > 0) {
    NSString *key = [temp objectAtIndex:0];
    NSLog(@"prayer: %@", key);
} else
    NSLog(@"NO Object Found");