/ /キャッチされていない例外 'NSRangeException'のためにアプリケーションを終了します。 [__NSArrayM objectAtIndex:]:境界33を越えるインデックス33 [0 .. 32] ' - objective-c、nsarray、nsrangeexception

未知の例外 'NSRangeException'のためにアプリケーションを終了しています。 [__NSArrayM objectAtIndex:]:境界33を越えるインデックス33 [0 .. 32] ' - objective-c、nsarray、nsrangeexception

私はこのImportViewController.mを持っています。

私は、抽出して、XMLからサーバーから名前をロードしています。 要素の総数は33です。

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"IMPORT";
NSLog(@"User id = %@",currentUserId);

//some code to send http request.............
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"str response:%@",str);
NSURL *fileURL= [[NSURL alloc] initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfURL:fileURL];
GDataXMLDocument *doc =[[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];

NSArray * names = [doc nodesForXPath:@"//contacts/contact/name/firstname" error:nil];

for (GDataXMLElement *element in names) {
// NSLog(@"name: %@ ",element.stringValue);
[Option addObject:element.stringValue];
}

[Option addObject:@"nil"];
NSLog(@"count: %u ",[Option count]);

for (CFIndex i=0; i<=[Option count]; i++) {
NSLog(@"options item %lu: %@n",i,Option[i]);
}
}

エラー:キャッチされていない例外「NSRangeException」の理由によるアプリケーションの終了、理由:* - [__ NSArrayM objectAtIndex:]:境界33を超えるインデックス33. [0 .. 32] " * ファーストスローコールスタック:

回答:

回答№1は4

あなたの行

for (CFIndex i=0; i<=[Option count]; i++)

読まなければならない

for (CFIndex i=0; i<[Option count]; i++)