如何将数组值分配给双变量

How to assign array values to double variable

我必须将数组值赋给 double variable.If 我喜欢这样, 它显示错误 -

Initializing 'double' with incompatible type 'id'.

我的代码:

NSArray *units =[[NSArray alloc]initWithObjects:@"20",@"4.0",@" 6.0",@"3.0",@" 12.0",@" 16.0",nil ];

 for (int i = 0; i < units.count; i++)
    {
        double yvalue = units[0];
        NSLog(@"val:%f",yvalue);
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:yvalue xIndex:i]];
    }

只需使用 doubleValue 转换对象

 double yvalue =[units[0] doubleValue];

这是因为你直接将string赋给double。
您需要将字符串转换为 double,如下所示:
double yvalue = [units[0] doubleValue];