如何从NSDictionary向NSarray添加键浮点值?

时间:2023-01-09 19:14:46

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

我的大脑是油炸的!我不能思考。我对iphone编程不熟

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:

正在做json解析....在那就是将数据从json存储到nsdictionary但是……我想将所有nsdictionary中的浮点值从字典中添加到数组中。这就是我现在正在做的。如下代码:

- (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.

请指导我,我没有犯错误。提前谢谢。

2 个解决方案

#1


1  

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.

你的应用程序崩溃了,因为你在向NSArray添加数据,这是静态数组,你不能在运行时添加值,所以只创建NSMutableArray并在NSMutableArray中添加数据。

#2


0  

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.

然后,您尝试将数组添加到它本身,这是没有意义的。但更糟糕的是,由于数组不再指向NSMutableArray,所以您甚至不能调用该方法。

[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];     
}

#1


1  

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.

你的应用程序崩溃了,因为你在向NSArray添加数据,这是静态数组,你不能在运行时添加值,所以只创建NSMutableArray并在NSMutableArray中添加数据。

#2


0  

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.

然后,您尝试将数组添加到它本身,这是没有意义的。但更糟糕的是,由于数组不再指向NSMutableArray,所以您甚至不能调用该方法。

[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];     
}