我搞砸了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!


因此,我认为我正在从数据库中一个接一个地获取数据(“ frase uno”,然后是“ frase due”,然后是“ frase tre”)。而且我还看到数组正在填充(已存储在数组中的短语:)

当我打电话时

prova = [frasiMemorizzate objectAtIndex:0];

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


我得到:“ Frase uno”,这是我应该期望的,但是如果我使用“ objectAtIndex:1”,则会出现该错误:索引1超出范围[0 .. 0],但是我希望使用第二个短语:“ Frase Due”。

我想知道为什么和正确的方式来获取我需要的数据,也许,当您回答我的问题时,我如何才能知道在可变数组中存储了多少个项目,从而无法调用未分配的索引位置!

谢谢你的耐心!

最佳答案

引发错误是因为您尝试在只有一个对象时尝试访问数组中的第二个索引。

进行一些小的更改即可使用:

    prova = [frasiMemorizzate objectAtIndex:i];

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

关于ios - NSMutableArray索引1超出范围[0 .. 0],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24228162/

10-09 01:10