user1581122
user1581122

Reputation:

how can adding Key float values from NSDictionary to NSarray?

My brain is fried! I can't think. i am new to iphone programming

am doing json parsing ....in that am storeing data from json to nsdictionary but ....... I want to add all nsdictionary float values from the dictionary to the array. This is what I am doing right now.As code below:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  [connection release];

   NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
   self.responseData = nil;

   dict = [responseString JSONValue];

            NSMutableArray *array = [NSMutableArray array];
           for (NSString *key in [dict allKeys])
          {

               array = [dict objectForKey:key];
           // array return float values but
        [array addObject:array ];  // geting carsh dude to array return float values like 120.01  

}

Please guide me i am not getting a part where i am doing a mistake. Thanks in advance.

Upvotes: 2

Views: 511

Answers (2)

Rajneesh071
Rajneesh071

Reputation: 31091

Your app is crashing because you are adding data to NSArray , this is static array, you can not add value at run time, so just Make NSMutableArray and add your data in NSMutableArray.

Upvotes: 1

driis
driis

Reputation: 164341

Your code is broken in a couple of ways.

This line assigns the array pointer to the object in the dictionary:

array = [dict objectForKey:key];

Then you are trying to add the array to itself, which does not make sense. But worse, since array does no longer point to your NSMutableArray you cannot even call that method.

[array addObject:array ]; 

You probably wanted to do something like this:

for (NSString *key in [dict allKeys])
{  
     id value = [dict objectForKey:key];              
     [array addObject:value];     
}

Upvotes: 0

Related Questions