2

I was trying to make an array of numbers. I need to call every element of this array. (array[i] ). I have done it like this:

NSNumber *array[] = {@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f};

but when I am calling this array it gives me an error:

Expected method to read array element not found on object of type "NSNumber"

Additional code moved from a comment:

int SIZE = 97;
fftw_complex *data, *fft_result, *ifft_result;
fftw_plan     plan_forward, plan_backward;
int           i;
NSArray * array = @[@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f];

float a0 = [array[0] floatValue];
data = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
fft_result  = ...
ifft_result = ...
plan_forward  = ...
plan_backward ...

for( i = 0 ; i < SIZE ; i++ ) {
    data[i][0] = array[i];
    data[i][1] = 0.0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
josef
  • 1,632
  • 1
  • 11
  • 16

2 Answers2

0

You are probably trying to initialise array, but in a "generics" fashion. Objective-C does not support generics.

Here's how you should initialise an NSArray of NSNumbers:

NSArray * array = @[@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f];
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • thank you for ur fast answer . i was doing it like this but i am getting this error beside it: ` NSArray * array = @{@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f}; Expected ; . ` pointing on the first comma between the first tow numbers. – josef May 20 '14 at 14:00
  • and by calling this array i am getting this error too: `Assigning to "double" from incompatible type "id"` – josef May 20 '14 at 14:04
  • 2
    @Eimantas The syntax is incorrect, for arrays use "[" and "]", for dictionaries use "{" and "}". The corrected statement: `NSArray * array = @[@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f]; ` – zaph May 20 '14 at 14:18
  • Fixed the syntax in the answer. – Zev Eisenberg May 20 '14 at 14:21
  • @Zaph but its still giving me the same error : 'data[i][0] = array[i][0]; Assigning to double from incompatible type id ' the element of the array musst have the type double too. – josef May 20 '14 at 14:27
  • What do you expect `array[i][0]` to do, it is not a two dimensional array. Additionally the number in the array is an `NSNumber` and must be unboxed. What you need is: `float a0 = [array[0] floatValue];`. – zaph May 20 '14 at 14:40
  • @zaph you are right, the problem is in my "data[i][0]" , the array is from type double. i am trying to mach this two arrays , data and array. by: for( i = 0 ; i < SIZE ; i++ ) { data[i][0] = array[i][0];//[self.array objectsAtIndexes:i ]; // stick your samples in here data[i][1] = 0.0; // use this if your data is complex valued } – josef May 20 '14 at 14:47
0

There are two problems

First the statement for creating the array has to errors, the "[]" after the variable and using "{}" instead of "[]" to initialize an array.

Then from a comment there is an incorrect access of the array items in two ways. The array is one diminutional, not two diminutional. Then the return from the array is an NSNumber and that must be unboxed, that is converted from an NSNumber to a float.

The following is example code with the errors corrected:

NSArray * array = @[@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f];
float a0 = [array[0] floatValue];
NSLog(@"a0: %f", a0);

NSLog output:

a0: 0.240128

Best bet: take the time to learn about NSArray access and Objective-C in general. There are many resources on the web both as manuals/documentation and tutorials.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • thank u for your passion. the array is working. may you please take a look on my code and tell me what i am doing false ? ` NSArray * array = @[@0.240128f , @0.240128f , @0.953934f , @1.181351f, @1.382523f, @1.497086f, @1.437790f , @0.851196f]; float a0 = [array[0] floatValue]; ... for( i = 0 ; i < SIZE ; i++ ) { data[i][0] = array[i]; data[i][1] = 0.0; }` – josef May 20 '14 at 15:01
  • I added the above code to the question, you can edit your own question. If it is not correct please edit as necessary. Please delete the above comment. – zaph May 20 '14 at 15:15
  • my main problem it was bye calling some data from an file.txt ( its in this Question: [link] (http://stackoverflow.com/questions/23748427/how-to-get-from-a-file-txt-the-array-that-i-need-in-x-code) i couldn't solve it so now i am trying to save the data in an array and to use it directly. – josef May 20 '14 at 15:22
  • may you please take a look (if you have time) on my new question ? [FFTw library](http://stackoverflow.com/q/23962954/3553742) – josef May 30 '14 at 22:46