0

I need to add a double array (as in double example[number of capacity];) to a nsmutablearray, but it seems it's harder than I thought. So I have:

NSMutableArray *sample; 
double ex[2];

as a global variable in my .m file

and in void method with two double paramaters which are example1 and example2 I am trying to

ex[0] = example1;
ex[1] = example2;

then add this ex array to nsmutablearray, however, I am getting error if I do this:

[sample addObject:ex];

somebody please help, I am also a newbie to this so I don't have much idea how. Thanks in advance!

I feel like I am not explaining myself that clear so I'll add this.

So basically, I want my mutablearray to look like this:

[[3.78,2.00], [4.6,8.90098], [67.9099, 56.788] ...]

like that 
4
  • Not sure but this is possibly a duplicate - please check this question and answer: stackoverflow.com/q/5469886/558933 . Does your ex array have to be a C array or can it be an NSArray or NSMutableArray? Commented Jul 27, 2015 at 1:00
  • it is a double array :) Commented Jul 27, 2015 at 1:01
  • { } denotes a dictionary, not an array. Commented Jul 27, 2015 at 1:09
  • oh.. I see! Thanks :) Commented Jul 27, 2015 at 1:14

2 Answers 2

4

Alternatively to Dasblinkenlight's answer, you could also use an NSArray of NSNumbers and add that to your NSMutableArray.


double example1 = 1.113;
double example2 = 129.74;
NSMutableArray *sample = [[NSMutableArray alloc] init];
NSArray *ex = @[[NSNumber numberWithDouble: example1], [NSNumber numberWithDouble: example2]];
[sample addObject: ex];
NSLog(@"example1: %f, example2: %f", ((NSNumber*)sample[0][0]).doubleValue, ((NSNumber*)sample[0][1]).doubleValue);
//logs example1: 1.113000, example2: 129.740000

You can add as many NSArrays of NSNumber as you want and use sample[][] to get the NSNumber and then easily unwrap it with doubleValue.

If you want both dimensions to be mutable, simply put the NSNumbers in mutable arrays instead.

Change this:

NSArray *ex = @[[NSNumber numberWithDouble: example1], [NSNumber numberWithDouble: example2]];

to this:

NSMutableArray* ex = [NSMutableArray arrayWithObjects: [NSNumber numberWithDouble: example1], [NSNumber numberWithDouble: example2], nil];
Sign up to request clarification or add additional context in comments.

3 Comments

so the sample [] [] is a 2D nsmutablearray? Interesting.. I'd try this :) thanks Metabble :)
sample would be a mutable array containing non-mutable arrays. So it's mutable in one dimension only. However, this is easily changed. I'll update the post.
This is great! Thanks a lot Metabble :)
2

Only Objective-C objects can be added to NSMutableArray. Since an array of doubles is not an Objective-C object, you need to wrap your array into something that can be placed in an Objective-C collection. For example, you can wrap it in NSData:

NSData *wrapped = [NSData dataWithBytes:ex, length:sizeof(ex)];
[sample addObject:wrapped];

Of course now you need to "unwrap" your array before accessing it:

NSData *wrapped = [sample objectAtIndex:...];
double* tmp = (double*)wrapped.bytes;
double x = tmp[0];

5 Comments

that worked! But when I try to nslog the sample nsmutablearray the double value is not appearing anymore and is something like this <00000000 00506c40 00000000 00487e40>
@jane This is because NSData does not know what's inside, so it prints raw bytes. If you retrieve the data by unwrapping, you would get your double values back.
Oh no.. :( I am trying this code double* tmp = (double*)wrapped.data; but I'm getting an error "Property 'data' not found on object of type'NSData *' " I feel so stupid right now, I'm going to try what Metabble suggested, but thank you very much for the effort dasblinkenlight! I appreciate it a lot!
@jane Sorry, the property is called bytes, not data.
oh I see! Thanks a lot dasblinkenlight :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.