How to create an NSMutableArray of floating point values - iphone

I'm new to Objective-C and iPhone development, and I'm trying to store floating-point values in an NSMutableArray, but when I do I get an error saying "incompatible type for argument 1 of 'addObject". What am I doing wrong? I'm trying to create an array of doubles that I can perform math calculations with.

NSMutableArray only holds objects, so you want an array to be loaded with NSNumber objects.
Create each NSNumber to hold your double then add it to your array. Perhaps something like this.
NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *num = [NSNumber numberWithFloat:10.0f];
[array addObject:num];
Repeat as needed.

Use an NSNumber to wrap your float, because the dictionary needs an object:
[myDictionary setObject:[NSNumber numberWithFloat:0.2f] forKey:#"theFloat"];
/* or */
[myDictionary setObject:#0.2f forKey:#"theFloat"];
retrieve it by sending floatValue:
float theFloat = [[myDictionary objectForKey:#"theFloat"] floatValue];
Code is untested.
You can wrap many other data types in NSNumber too, check the documentation. There's also NSValue for some structures like NSPoint and NSRect.

In Cocoa, the NSMutableDictionary (and all the collections, really) require objects as values, so you can't simply pass any other data type. As both sjmulder and Ryan suggested, you can wrap your scalar values in instances of NSNumber (for number) and NSValue for other objects.
If you're representing a decimal number, for something like a price, I would suggest also looking at and using NSDecimalNumber. You can then avoid floating point inaccuracy issues, and you can generally use and store the "value" as an NSDecimalNumber instead of representing it with a primitive in code.
For example:
// somewhere
NSDecimalNumber* price = [[NSDecimalNumber decimalNumberWithString:#"3.50"] retain];
NSMutableArray* prices= [[NSMutableArray array] retain];
// ...
[prices addObject:price];

NSMutableArray *muArray = [[NSMutableArray alloc] init];
NSNumber *float = [NSNumber numberWithFloat:210.0f];
NSNumber *float1 = [NSNumber numberWithFloat:211.0f];
[muArray addObject:float];
[muArray addObject:float1];
NSlog(#"my array is--%#",muArray);

Related

iPhone: How to convert normal NSArray into an NSArray containing NSNumber values?

In my iPhone app, I am using an array which contains the values like shown below:
A:{
1000,
2000,
-1000,
4000
}
Now I want to convert this to the array shown below.
NSArray *A = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1000],[NSNumber numberWithInt:2000],[NSNumber numberWithInt:-1000],[NSNumber numberWithInt:4000],nil];
How can I do that?
Edit:
Also I cannot use the value of Array A:{1000,2000,-1000,4000} to directly pass it into the NSNumber numberWithInt method, because it takes these values as NSString and not integers.
regarding to another question I saw from you I'm guessing those values are NSStrings
then use something like this.
NSMutableArray *array = [NSMutableArray array];
for (NSString *numberString in numberStringArray) {
[array addObject:[NSNumber numberWithInteger:[numberString integerValue]]];
}
and to be honest I think you should invest more time for the basics before you try to make use of core-plot

How can I use an integer value as 'key' to set value in NSMutableDictionary?

How can I use an integer value as 'key' to set a float value in NSMutableDictionary ?
As NSDictionarys are only designed to deal with objects, a simple way to do this is to wrap the integer and float in a NSNumber object. For example:
NSMutableDictionary *testDictionary = [[NSMutableDictionary alloc] init];
[testDictionary setObject:[NSNumber numberWithFloat:1.23f]
forKey:[NSNumber numberWithInt:1]];
NSLog(#"Test dictionary: %#", testDictionary);
[testDictionary release];
To extract the relevant value, simply use the appropriate intValue, floatValue, etc. method from the NSNumber class.
You can use NSMapTable as it supports integer keys and/or values directly. No need to box/unbox through NSNumber, but it is also slightly more difficult to set up and use.
It needs to be an object, so use [NSNumber numberWithInt:myInteger] instead.
Then, retrieve it with -integerValue

Creating a Two-Dimensional Array with Concrete Positions

I need to create a custom array:
In php I would define as follows:
$myarray[100][80] = 1;
But I don't know how to do it in objective-c...
I don't need an array [0][0],[0][1],[0][2], ... I only need concrete positions in this array [80][12], [147][444], [46][9823746],...
The content of these positions always will be = 1;
for this you would use a dictionary rather than an array as they are always 0,1,2 keyed so something along the lines of:
NSNumber *one = [NSNumber numberWithInt:1];
NSString *key = #"80,12";
NSDictionary *items = [NSDictionary dictionaryWithObject:one forKey:key];
Then to pull them out again you would use the objectForKey: method.
You cannot put ints directly into arrays or dictionaries that's why it is wrapped in the NSNumber object. To access the int after getting the NSNumber out of the dictionary you would use something like:
NSNumber tempNum = [items objectForKey:key];
int i = tempNum.intValue;
See the docs here for a full explanation of the NSDictionary class. Hope this helps...
I an not a PHP master but I believe in php arrays are not real arrays they are hash tables right?
Anyway, I think you are looking for NSDictionary or NSMutableDictionary class.
That looks more like a bitset than an array.
Allocating so many cells for that seems useless, so maybe you could revert the problem, and store the positions in an array.
Well in objective c we can use NSMutableArray to define 2-D arrays.
See the following code, it might help you
NSMutableArray *row = [[NSMutableArray alloc] initWithObjects:#"1", #"2", nil];
NSMutableArray *col = [[NSMutableArray alloc] init];
[col addObject:row];
NSString *obj = [[col objectAtIndex:0] objectAtIndex:0];
NSLog(#"%#", obj);

Mutable Object within an Immutable object

Is it acceptable to have a NSMutableArray within an NSDictionary? Or does the NSDictionary also have to be mutable?
The NSMutableArray will have values added to it at runtime, the NSDictionary will always have the same 2 NSMutableArrays.
Thanks,
Dan
Yes, it's perfectly acceptable. Keep in mind, the contents of the array are the pointers to your NSMutableArrays--those are what can't change in the immutable dictionary structure. What the pointers point to can change all you want. To wit:
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary *dict = [NSDictionary dictionaryWithObject:arr forKey:#"test"];
[arr addObject:#"Hello"];
NSString *str = [[dict objectForKey:#"test"] objectAtIndex:0];
NSLog("%#", str);
It's quite acceptable. But, it's precisely the sort of setup that suggests you should seriously consider replacing the dictionary with an NSObject subclass that sports two properties for accessing the arrays.

Storing ints in a Dictionary

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, right?
SOo...
NSNumber *testNum = [NSNumber numberWithInt:varMoney];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:#"OMG, Object 1!!!!" forKey:#"1"];
[dictionary setObject:#"Number two!" forKey:#"2"];
[dictionary setObject:testNum forKey:#"3"];
NSNumber *retrieved = [dictionary objectForKey:#"3"];
int newVarMoney = [retrieved intValue];
Where varMoney is an int that has been declared earlier. My question is, is there a better way to store "int" in a dictionary than putting it into a NSNumber?
Thanks!
Edit: 04/25/13
It's been a long time since I asked this question. For people stumbling on it in the future, there are easier ways to do this with the Apple LLVM Compiler 4.0, which has been default in Xcode for a bit. (ARC)
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:#1337 forKey:#"1"];
That's it, use the #1337 Syntax to quickly create NSNumber objects. Works with Variables, so my above could become:
[dictionary setObject:#(varMoney) forKey:#"3"];
or
dictionary[#"mykey"] = #1337;
Simpler.
You are correct, NSNumber is the normal way to handle this situation. You can use NSValue or NSDecimalNumber, too.
If you are prepared to fall back to using Core Foundation (CFDictionaries) you can store anything you like. I've definitely made dictionaries into which I put arbitrary void* values.
You need to do a little more plumbing but not much, and its all documented.
Sorry but no :( Not as far as I know. Reason being that integers aren't objects, and NSDictionary only stores objects.