NSMutableArray索引1超出边界[0 .. 0]

时间:2014-06-15 09:15:39

标签: ios objective-c arrays nsmutablearray

我搞砸了JSON和NSMutablearray。我当然是初学者。

我有这个代码,为了我的目的从在线教程中采取和修改:

[This is the .m file]
- (void) downloadItems {

    // Download the json file
    NSURL *jsonFileUrl = [NSURL URLWithString:@"http://example.com/service.php"];

    // Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

    // Create the NSURLConnection
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Initialize the data object
    datiScaricati = [[NSMutableData alloc] init];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the newly downloaded data
    [datiScaricati appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    // Create an array to store the locations
    NSMutableArray *frasiMemorizzate = [[NSMutableArray alloc] init];

    // Parse the JSON that came in
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:datiScaricati options:NSJSONReadingAllowFragments error:&error];


    // Loop through Json objects, create question objects and add them to our questions array
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSDictionary *jsonElement = jsonArray[i];

        // Create a new location object and set its props to JsonElement properties
        Frase *nuovaFrase = [[Frase alloc] init];
        nuovaFrase.fraseDelDatabase = jsonElement[@"Frase"];

        NSLog(@"Phrase read from database: %@", nuovaFrase.fraseDelDatabase);

        // Add this question to the locations array
        [frasiMemorizzate addObject:nuovaFrase.fraseDelDatabase];

        NSLog(@"Phrases stored in the array: %@", frasiMemorizzate);


        prova = [frasiMemorizzate objectAtIndex:0];

        NSLog(@"Phrases at index 0: %@",prova);

    }


}

NSLog给出了这个:

2014-06-15 11:05:28.705 ProvaMySql[13805:60b] Phrase read from database: Frase uno, Prova!
2014-06-15 11:05:28.706 ProvaMySql[13805:60b] Phrases stored in the array: (
    "Frase uno, Prova!"
)
2014-06-15 11:05:28.706 ProvaMySql[13805:60b] Phrases at index 0: Frase uno, Prova!
2014-06-15 11:05:28.707 ProvaMySql[13805:60b] Phrase read from database: Frase due, Prova!
2014-06-15 11:05:28.707 ProvaMySql[13805:60b] Phrases stored in the array: (
    "Frase uno, Prova!",
    "Frase due, Prova!"
)
2014-06-15 11:05:28.708 ProvaMySql[13805:60b] Phrases at index 0: Frase uno, Prova!
2014-06-15 11:05:28.708 ProvaMySql[13805:60b] Phrase read from database: Frase tre, Prova!!
2014-06-15 11:05:28.709 ProvaMySql[13805:60b] Phrases stored in the array: (
    "Frase uno, Prova!",
    "Frase due, Prova!",
    "Frase tre, Prova!!"
)
2014-06-15 11:05:28.709 ProvaMySql[13805:60b] Phrases at index 0: Frase uno, Prova!

所以,我认为我从数据库中逐个获取数据(&#34; frase uno&#34;然后&#34; frase due&#34;然后&#34; frase tre&#34;) 。我还看到数组正在填充(Phrased存储在数组中:)

当我打电话时

prova = [frasiMemorizzate objectAtIndex:0];

        NSLog(@"Phrases at index 0: %@",prova);

我得到:&#34; Frase uno&#34;,这就是我应该期待的,但如果我使用&#34; objectAtIndex:1&#34;我有这个错误:索引1超出了界限[0 .. 0],但我希望有第二个短语:&#34; Frase Due&#34;。

我想知道为什么以及获得我需要的数据的正确方法,也许,当你回答我的问题时,我怎么知道有多少项存储在Mutable数组中,非调用未分配的索引位置!

感谢您的耐心等待!

1 个答案:

答案 0 :(得分:1)

抛出错误,因为当只有一个对象时,您尝试访问数组中的第二个索引。

进行这一点改动并且有效:

    prova = [frasiMemorizzate objectAtIndex:i];

    NSLog(@"Phrases at index %d: %@", i, prova);