IOS应用程序崩溃:[__ NSArrayI objectAtIndex:]:索引0超出空数组的边界

时间:2014-12-02 06:15:46

标签: ios nsarray segue nsmanagedobject

我有一种方法可以传递一个' id'进入这里:

NSString *id = @"4bf58dd8d48988d181941735";
[self getNames:id];

这很好用,但是当我使用' id'来自在segue内传递的对象:

NSString *id = _selectedStore._id;
[self getNames:id];

我收到错误:

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'

发生了什么?

以下是收到错误的地方。我的尝试(请记住我是新手)是获取一系列Foursquare ID,获取名称,然后获取idUrl的id。如果我删除了创建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);
              }
        }];
    }
}

2 个答案:

答案 0 :(得分:2)

你的问题可能就在这里:

NSString *prefix = 
 [[self.venuePhotos valueForKeyPath:@"items.prefix"] objectAtIndex:0];

你应该这样做:

if ([[self.venuePhotos valueForKeyPath:@"items.prefix"] count] > 0) {
NSString *prefix = 
 [[self.venuePhotos valueForKeyPath:@"items.prefix"] objectAtIndex:0]
...
} else {
// Do something for not having the prefix
}

如果您想要超级安全,这总是好的,请执行此操作

if ([[self.venuePhotos valueForKeyPath:@"items.prefix"] 
                         isKindOfClass:[NSArray class]] 
 && [[self.venuePhotos valueForKeyPath:@"items.prefix"] count] > 0)

这也将确保该项目是一个数组。如果1 [self.venuePhotos valueForKeyPath:@&#34; items.prefix&#34;] 1不是并且它没有响应计数,它将崩溃。

在使用索引访问数组之前,很少遵循的通用经验法则始终是检查边界。无论你是通过下标还是objectAtIndex得到它。

答案 1 :(得分:0)

 id object =  __NSArrayI[i];

NSUInteger index = [__NSArrayI indexOfObject:object];

试试这个......首先检查你的阵列数量。