Search code examples
iosobjective-cnsarray

How to solve this " index 0 beyond bounds for empty array! "


I am having a problem when checking if there is something coming from database:

Code:

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!!!");
}

my query:

-(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;
}

The error appears when I check whether it is empty or if something is returned.


Solution

  • test[0] is already trying to subscript the array. You need to check for the count property:

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