二进制表达式的操作数无效('NSMutableArray'和'double')

时间:2015-10-18 11:22:46

标签: ios objective-c

我想将double值存储到Array中,然后组合起来计算一些结果但我遇到了一些错误。还有其他办法吗?

NSMutableArray *storeImpedance;
NSMutableArray *storeLength;

double designFrequency = 1e9;
double simulateFrequency = 1.5e9;
double pi = 3.14159265359;
double omega = 2*pi*simulateFrequency;
double Z0=50;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    storeImpedance = [[NSMutableArray alloc]initWithCapacity:25];
    storeLength = [[NSMutableArray alloc]initWithCapacity:25];
}

- (IBAction)addButton:(UIButton *)sender {
    storeCount++;
    [storeImpedance addObject:[NSNumber numberWithDouble:[impedanceTextField.text doubleValue]]];
    [storeLength addObject:[NSNumber numberWithDouble:[lengthTextField.text doubleValue]]];

}

if (imageIndex==1)
{
      thetarad=storeImpedance*pi/180*simulateFrequency/designFrequency;
      A=cos(thetarad);
      B=I* storeImpedance*sin(thetarad);
      C=I*sin(thetarad)/storeImpedance;
      D=cos(thetarad);
}

1 个答案:

答案 0 :(得分:0)

在这一行:

hetarad=storeImpedance*pi/180*simulateFrequency/designFrequency;

您使用的storeImpedance类型为NSMutableArray,此表达式中的预期类型为double

从数组中提取所需的double值并使用它来解决问题:

NSNumber *number = (NSNumber *)[storeImpedance firstObject]; // Or use objectAtIndex: for a specific value in the array if it is not the first one
double value = [number doubleValue];

hetarad=value*pi/180*simulateFrequency/designFrequency;

如果要为数组中的每个值计算hetarad

for (id element in storeImpedance) {
    NSNumber *number = (NSNumber *)element 
    double value = [number doubleValue]; 

    hetarad=value*pi/180*simulateFrequency/designFrequency;
    // You should do something with hetarad here (either store it or use in other required logic otherwise it will be overwritten by the next iteration
}