AddObject into Array

Hello Everyone,


I'm trying to add object into an mutable array, but I'm facing some issue here.

Here's my code:


            for(int i = 0; i<[json count]; i++)
            {
                gg *ob = [[gg alloc] init];
             
                ob.id = [json valueForKeyPath:@"id"];
                ob.name = [json valueForKeyPath:@"name"];
                ob.age = [json valueForKeyPath:@"age"];
             
                [self.arrayObj addObject: ob];
            }



Program executed smoothly, but nothing is added into arrayObj.

Is self.arrayObj nil at that point?


If not, you will have to show more code - all the other code that declares, initializes, and modifies arrayObj, with enough context to know where those bits of code are called from. And the part that shows how you are determining that "nothing is added".

We need more information, as junkpile says, but this code looks wrong.


Since you're looping [json count] times, json seems to be some kind of collection, perhaps an array. But you don't retrieve any particular element inside the loop. At the very least, you're going to create a lot of identical 'gg' objects.


Don't you need code something like the following?


for(int i = 0; i<[json count]; i++) 
{ 
     SomeClass* j = [json objectAtIndex: i]; // Or something like that

     gg *ob = [[gg alloc] init]; 

     ob.id = [j valueForKeyPath:@"id"]; // Referencing the element you got earlier
     ob.name = [j valueForKeyPath:@"name"]; 
     ob.age = [j valueForKeyPath:@"age"]; 

     [self.arrayObj addObject: ob]; 
}


The other possibility is that [json count] is zero.

Yes, self.arrayObj is nil at this point. It's empy array that's ready to store the object "ob"


I found out that my json send all the elements "id" together at once to ob.id , all elements "name" together at once to "ob.name" and same goes to age.


What I'm trying to do here is, getting only first element of ID, Name and Age and save into ob.id, ob.name and ob.age. I declared id, name and age's data type as NSString. I guess the problem for now is.. it failed to store all the elements retrieved from json, because NSString meant to store a string only? Not a bunch of seperated strings.


I'm trying to modify how my JSON read now. Trying to get those elements one by one, instead of getting it once at a time. Hopefully im doing it right? *Researching. . . . .

The json is a json result from web service. I received elements from there, but the problem is Im getting a bunch of elements into a NSString which I guess it does not work this way. **Details in my replied to junkpile


My intention is to store each element into different objects, meaning if I have 10 elements result from JSON. I will be having 10 objects created.

> Yes, self.arrayObj is nil at this point. It's empy array that's ready to store the object "ob"


nil is nil. nil is not a reference to any object, particularly not an empty array. If you need an empty array, then you need to initialize it as such somewhere before you try and store data into it, e.g.


self.arrayObj = [NSMutableArray array];


and yes as Quincey says, the code you posted is missing a part where it uses the loop counter to index into whatever is in your JSON response.

>> self.arrayObj is nil at this point. It's empy array that's ready to store the object "ob"


No, no! 🙂


A nil object isn't an empty array. You have to start by creating an actual array with 0 elements.


>> I found out that my json send all the elements "id" together at once to ob.id , all elements "name" together at once to "ob.name" and same goes to age.


I'm just guessing, but I suspect this isn't true. Since you reference '[json count]', it looks like 'json' is actually a NSArray, and I bet it's an array of NSDictionary objects. (That's the sort of thing you would normally get back from [NSJSONSerialization JSONObjectWithData:…].


If you send a '[valueForKeyPath: @"name"]' message to a NSArray, it actually tries sending a 'name' message to each element of the array, for reasons that have nothing to do with JSON. If the array elements are dictionaries, that will retrieve the value for key "name" from the dictionary. The results are combined into a new NSArray containing the names.


If I'm right, your code should actually look something like this:


for(int i = 0; i<[json count]; i++)
{
     NSDictionary* j = [json objectAtIndex: i];

     gg *ob = [[gg alloc] init];

     ob.id = [j objectForKey: @"id"];
     ob.name = [j objectForKey: @"name"];
     ob.age = [j objectForKey: @"age"];

     [self.arrayObj addObject: ob];
}


Actually, it would be even better to "modernize" your syntax, and rewrite in this equivalent form:


for(NSUInteger i = 0; i<json.count; i++)
{
     NSDictionary* j = json [i];

     gg *ob = [[gg alloc] init];

     ob.id = j [@"id"];
     ob.name = j [@"name"];
     ob.age = j [@"age"];

     [self.arrayObj addObject: ob];
}


It means exactly the same thing, but it's more readable.

AddObject into Array
 
 
Q