2015-10-18 92 views
-2

我想將double值存儲到Array中,稍後將計算一些結果,但遇到一些錯誤。還有另一種方法可以做到嗎?對二進制表達式無效的操作數('NSMutableArray'和'double')

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); 
} 
+5

'storeImpedance'是一個NSMutableArray - 你爲什麼會認爲你可以乘以一倍? – Paulw11

回答

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 
} 
+0

如果我想使用數組中的所有值,我該怎麼做? – Jason

+0

@Jason看看更新的答案 – giorashc

+0

如果你有2個數組變量元素,我可以把2個id放在for循環中嗎? – Jason

相關問題