Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 如何解决这个问题;索引0超出空数组的界限&引用;_Ios_Objective C_Nsarray - Fatal编程技术网

Ios 如何解决这个问题;索引0超出空数组的界限&引用;

Ios 如何解决这个问题;索引0超出空数组的界限&引用;,ios,objective-c,nsarray,Ios,Objective C,Nsarray,我在检查是否有来自数据库的内容时遇到问题: 代码: hourPrograma: @"12:00"; NSArray *hourLinha = [hourProgram componentsSeparatedByString:@":"]; NSArray * test = [notificationDAO selectHourMin:[hourLinha[0] intValue]:[hourLinha[1] intValue]]; if (!test[0]) { NSLog(@"ok!!

我在检查是否有来自数据库的内容时遇到问题:

代码:

hourPrograma: @"12:00";
NSArray *hourLinha = [hourProgram componentsSeparatedByString:@":"];

NSArray * test = [notificationDAO selectHourMin:[hourLinha[0] intValue]:[hourLinha[1] intValue]];

if (!test[0]) {
   NSLog(@"ok!!!");
} else {
   NSLog(@"empty!!!");
}
我的问题是:

-(NSArray *) selectHourMin:(NSInteger *) hour: (NSInteger *) min {
    query = [NSString stringWithFormat:@"SELECT hour, min FROM notification WHERE %i = hour AND %i = min", hour, min];
    NSArray * resp = [self loadDataFromDB:query];
    return resp;
}
当我检查它是否为空或是否返回了内容时,会出现错误。

将测试[0]替换为

test.count > 0

test[0]
已在尝试为数组下标。您需要检查
count
属性:

if (test.count) {
    // ...
}

您需要检查结果,如下所示:

if (test.count > 0) 
{    
  NSLog(@"ok!!!"); 
} 
else 
{    
  NSLog(@"empty!!!"); 
}
希望这会有所帮助。

或在函数中使用block:)

打电话

hourPrograma: @"12:00";
NSArray *hourLinha = [hourProgram componentsSeparatedByString:@":"];

NSArray * test = [notificationDAO selectHourMin:[hourLinha[0] intValue]:[hourLinha[1] intValue]];

[self selectHourMin:1 :2 success:^(NSArray *result) {
    NSLog(@"result: %@", result);
} failure:^(NSString *error) {
    NSLog(@"%@", error);
}];

您的
if
语句错误。在所有情况下,您都在检查第一个对象。如果没有记录,并且您的
测试
对象是
nil
,该怎么办。这将导致您面临的崩溃。请使用
Array
属性
count
。顺便说一句-您的方法名为:
selectHourMin::
。这是一个糟糕的命名约定。您应该将方法命名为:
'selectHour:andMin:
hourPrograma: @"12:00";
NSArray *hourLinha = [hourProgram componentsSeparatedByString:@":"];

NSArray * test = [notificationDAO selectHourMin:[hourLinha[0] intValue]:[hourLinha[1] intValue]];

[self selectHourMin:1 :2 success:^(NSArray *result) {
    NSLog(@"result: %@", result);
} failure:^(NSString *error) {
    NSLog(@"%@", error);
}];