如何解决这个“空数组的索引 0 超出范围!”
How to solve this " index 0 beyond bounds for empty array! "
我在检查是否有来自数据库的内容时遇到问题:
代码:
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!!!");
}
希望这会有所帮助。
或者在函数中使用块:)
-(void) selectHourMin:(NSInteger *) hour: (NSInteger *) min success:(void (^)(NSArray *result))successBlock failure:(void (^)(NSString * error))failureBlock{
query = [NSString stringWithFormat:@"SELECT hour, min FROM notification WHERE %i = hour AND %i = min", hour, min];
NSArray * resp = [self loadDataFromDB:query];
if (resp.count > 0 ) {
successBlock(resp);
}else{
failureBlock(@"empty result");
}
}
并致电
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);
}];
我在检查是否有来自数据库的内容时遇到问题:
代码:
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!!!");
}
希望这会有所帮助。
或者在函数中使用块:)
-(void) selectHourMin:(NSInteger *) hour: (NSInteger *) min success:(void (^)(NSArray *result))successBlock failure:(void (^)(NSString * error))failureBlock{
query = [NSString stringWithFormat:@"SELECT hour, min FROM notification WHERE %i = hour AND %i = min", hour, min];
NSArray * resp = [self loadDataFromDB:query];
if (resp.count > 0 ) {
successBlock(resp);
}else{
failureBlock(@"empty result");
}
}
并致电
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);
}];