2014-06-15 83 views
0

我搞砸了JSON和NSMutablearray。當然我是初學者。NSMutableArray索引1越界[0 .. 0]

我有這樣的代碼,從一個在線教程,採取和修改,我的目的:

[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! 

所以,我認爲,我被一個獲得數據的一個從數據庫(「刪除uno」,然後「frase due」,然後「frase tre」)。而且我還看到該陣列填充(措辭存儲陣列:)

。當我打電話

prova = [frasiMemorizzate objectAtIndex:0]; 

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

我得到:「Frase UNO」,這是我應該期待什麼,但如果我使用「objectAtIndex:1」我有這樣的錯誤:索引1超越邊界[0 .. 0],但我希望有第二個短語:「Frase Due」。

我想知道爲什麼和正確的方式來獲取我需要的數據,也許,當你回答我的問題,我怎麼知道有多少項存儲在一個可變數組中,非調用未分配的指數位置!

感謝您的耐心等待!

回答

1

由於在只有一個對象時嘗試訪問數組中的第二個索引,因此會引發錯誤。

讓這個小小的變化,它的工作原理:

prova = [frasiMemorizzate objectAtIndex:i]; 

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

感謝Juniperi,是的,現在它的工作原理!但是如果我只需要選擇三個短語中的一個呢?我認爲在陣列中存儲了所有這三個短語,然後選擇一個我需要的! – Funesto

+0

@Funesto你可以選擇任何你想要的對象後循環。你的for循環每次將一個對象放到數組中,所以你不能在第一輪訪問第二個對象。 – juniperi

+0

-_-非常感謝。我不僅是初學者,我也是愚蠢的!現在它工作。謝謝你的教導!現在我將搜索如何知道Mutable Array中有多少個索引! – Funesto

相關問題