【发布时间】:2014-12-02 06:15:45
【问题描述】:
我有一个方法可以在这里传递一个“id”:
NSString *id = @"4bf58dd8d48988d181941735";
[self getNames:id];
这很好用,但是当我使用 segue 中传递的对象中的“id”时:
NSString *id = _selectedStore._id;
[self getNames:id];
我得到错误:
'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
发生了什么事?
这里是它得到错误的地方。我的尝试(请记住,我是新手)是获取 Foursquare id 的数组,获取名称,然后获取 id 的 photoUrl。如果我删除所有创建 photoUrl 的代码(我在代码中已注明),它会运行而不会崩溃。
- (void)getNames {
NSMutableArray *final = [[NSMutableArray alloc] init];
for (SearchVenue *object in self.venues) {
[Foursquare2 venueGetDetail:object._id callback:^(BOOL success, id result){
if (success) {
NSDictionary *dic = result;
NSArray *venue = [dic valueForKeyPath:@"response.venue"];
self.venueDetail = venue;
NSMutableDictionary *newVen = [NSMutableDictionary dictionary];
[newVen setValue:[self.venueDetail valueForKey:@"name"] forKey:@"name"];
[final addObject:newVen];
[Foursquare2 venueGetPhotos:object._id limit:nil offset:nil callback:^(BOOL success, id result){
if (success) {
NSDictionary *dic = result;
NSArray *photos = [dic valueForKeyPath:@"response.photos"];
self.venuePhotos = photos;
//works when removed from here...
NSString *prefix = [[self.venuePhotos valueForKeyPath:@"items.prefix"] objectAtIndex:0];
NSString *size = @"100x100";
NSString *suffix = [[self.venuePhotos valueForKeyPath:@"items.suffix"] objectAtIndex:0];
NSArray *myStrings = [NSArray arrayWithObjects:prefix,size,suffix, nil];
NSString *photoUrl = [myStrings componentsJoinedByString:@"" ];
//...to here
NSMutableDictionary *newVen1 = [NSMutableDictionary dictionary];
[newVen1 setValue:photoUrl forKey:@"photoUrl"];
for(int i = 0; i < [final count]; i++) {
[[final objectAtIndex:i] addEntriesFromDictionary:newVen1];
}
_arrayOfPlaces = final;
NSLog(@"_arrayOfPlaces is %@",_arrayOfPlaces);
[self.resultsVC reloadData];
} else {
NSLog(@"%@",result);
}
}];
} else {
NSLog(@"%@",result);
}
}];
}
}
【问题讨论】:
-
在检查索引处的对象之前检查数组计数。
-
"id" 是 Objective-C 的内置关键字。您可以尝试将您的字符串名称更改为 valId 或其他任何名称。
-
我将其更改为“识别”,但仍然出现错误。
-
您应该发布更多错误消息。例如,任何堆栈跟踪。什么是 getNames?
-
发布您的 getNames 方法的代码。
标签: ios nsarray segue nsmanagedobject