Reputation: 67
I am messing up with JSON and NSMutablearray. Of course I am a beginner.
I have this code, taken and modified for my purposes from an online tutorial:
[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 gives this:
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!
So, I think that I am getting data one by one from database ("frase uno", then "frase due", then "frase tre"). And I see also that the array is populating (Phrased stored in the array:)
When I am calling
prova = [frasiMemorizzate objectAtIndex:0];
NSLog(@"Phrases at index 0: %@",prova);
I get: "Frase uno", that is what I should expect, but if I use "objectAtIndex:1" I have that error: index 1 beyond bounds [0 .. 0], but I expected to have the second phrase: "Frase Due".
I'd like to know why and the right way to get the data I need, and, maybe, as you are replying my question, how can I Know how many items are stored in a Mutable Array, to non call unallocated index position!
Thanks for your patience!
Upvotes: 0
Views: 148
Reputation: 3763
Error is thrown because you try to access second index in the array when there is only one object.
Make this little change and it works:
prova = [frasiMemorizzate objectAtIndex:i];
NSLog(@"Phrases at index %d: %@", i, prova);
Upvotes: 1