How do I access floats from a dictionary of floats? - objective-c

I have a property list (Data.plist) that contains an array of two dictionaries. Each dictionary is filled with key names (Factor 1, Factor 2, etc) and floats (0.87, 1.15, etc.). I am trying to access the numbers stored in the dictionary. First I load the dictionary using:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Data.plist"];
NSArray *plistData = [[NSArray arrayWithContentsOfFile:finalPath] retain];
NSDictionary *dictionaryOne = [plistData objectAtIndex:0];
Actually accessing the numbers stored is where I'm having a problem:
Float32 conversionFactor = [scyToLCMMen objectForKey:"Factor 50"];
I'm getting the error: "incompatible types in initialization". What am I doing wrong? Is it not a Float32?

Objective-c containers can only hold obj-c types, so what you get is not a float for sure. What you probably have is NSNumber object and you need to "extract" plain float value from it:
Float32 conversionFactor = [[scyToLCMMen objectForKey:"Factor 50"] floatValue];

NSNumber *conversionFactor = [dictionaryOne valueForKey:#"Factor 50"];
float factor = [conversionFactor floatValue];
That's what I'd do

The value is likely an instance of NSNumber.
Float32 conversionFactor = [[scyToLCMMen objectForKey:"Factor 50"] floatValue];

Related

Create array of floats from csv Objective C

I have once again a beginner problem. I have a CSV file that looks something like this:
3.4,2.4,6.30,2.2,53.42,54,1,5
Now, I have a code that can parse this into an array
NSError *error;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"csv_file" ofType:#"csv" inDirectory:nil];
NSString *string = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
NSArray *array = [array componentsSeparatedByString:#","];
The issue I have is that I can't do math with these numbers (because they are char - or maybe string, not sure -).
My question is, Is there a way like I did but to create the array with floats, or is there a way to make the strings (or chars) in array into floats.
Thank you, and of course if my question isn't clear just let me know.
Let the elements in the array remain instances of NSString (this is what they are). Just when you access an element from the array make it a float like this:
float f = [array[index] floatValue];
You can't have NSArray of floats in Objective-C because NSArray may contain objects only.
You may be looking for the floatValue property
float sum = 0;
for (NSString *numberString in array) {
sum += [numberString floatValue];
}
If you want to put them in a C array:
float floatArray[array.count];
for(i = 0; i < sizeof(floatArray); i++) {
NSString *numberString = array[i];
floatArray[i] = [numberString floatValue];
}
Note that this way of creating a c array will add it to the stack; you'll need to use malloc if you want to add it to the heap.

Multidimensional Arrays in Objective C

I have an NSDictionary filled with data. If this was php it might be accessed by  something like:
$data = $array['all_items'][0]['name'];
How do I do something similar in objective c? ($array would be an NSDictionary)
The equivalent code in Objective-C is:
id data = [[[array objectForKey:#"all_items"] objectAtIndex:0] objectForKey:#"name"];
Note that objectForKey: is a method of NSDictionary, and objectAtIndex: is a method of NSArray.
A shortcut in Xcode 4.5, using LLVM 4.1, is:
id data = array[#"all_items"][0][#"name"];
Also note that if "array" is an NSDictionary instance and you want to get an array of all values in the dictionary, you use the allValues method of NSDictionary:
id data = [array allValues][0][#"name"];
Of course, allValues returns an unsorted array, so accessing the array by index is not very useful. More typically, you'd see:
for (NSDictionary* value in [array allValues])
{
id data = value[#"name"];
// do something with data
}
Unfortunately the objective-c version is not as elegant syntactically as the PHP version:
NSDictionary *array = ...;
NSArray *foo = [array objectForKey#"all_items"];
NSDictionary *bar = [foo objectAtIndex:0];
NSString *data = [bar objectForKey#"name"];
For brevity, you can do this on a single line as:
NSString *data = [[[array objectForKey#"all_items"] objectAtIndex:0] objectForKey#"name"];
You should use it,
NSString *value = [[multiArray objectAtIndex:1] objectAtIndex:0];
You can look the question here

pointer to integer

I have an array of integer and i'm trying to get an element from the array; xcode keeps showing this message: "initialization makes integer from pointer without a cast"
I know that this warning means that i can't alloc integer to pointer type, and i'm asking how ca i get that element, or if there is a solution to convert pointer to integer
Here is my code:
NSString *pathvalidrep = [[NSBundle mainBundle] pathForResource:#"validrep" ofType:#"plist"];
NSArray *tabreponses = [[NSArray arrayWithContentsOfFile:pathvalidrep] retain];
truerep = tabreponses;
[tabreponses release];
NSUInteger val1 = [truerep objectAtIndex:0]; //the warning apears here
thanx for help :)
Elements of the array are probably NSNumber objects... And the warning is appearing because of this, you assign a pointer to NSNumber to a NSUInteger.
Try :
NSUInteger val1 = [[truerep objectAtIndex:0] integerValue];

How can I create an NSArray with float values

I want to make a float value array. How can I do this? My code is:
NSArray *tmpValue = [[NSArray alloc] init];
total = total + ([[self.closeData objectAtIndex:i]floatValue] - total)* expCarpan;
firstValue = total;
NSArrays only take object types. You can add various non-object types to an NSArray by using the NSNumber wrapper:
NSNumber *floatNumber = [NSNumber numberWithFloat:myFloat];
[myArray addObject:floatNumber]; // Assuming `myArray` is mutable.
And then to retrieve that float from the array:
NSNumber *floatNumber = [myArray objectAtIndex:i];
float myFloat = [floatNumber floatValue];
(As you have done in your code above).
Update:
You can also use the NSValue wrapper in the same way as NSNumber for other non-object types, including CGPoint/Size/Rect/AffineTransform, UIOffset/EdgeInsets and various AV Foundation types. Or you could use it to store pointers or arbitrary bytes of data.
The NSArray class can only contain instances of other Objective-C objects. Fortunately, Apple already has several Objective-C object types for encapsulating C primitive types. For instance, NSNumber can incapsulate many different types of C numbers (integers, floats, etc.). NSValue can incapsulate arbitrary structures, CGPoints, pointers, etc. So, you can use NSNumber and float in conjunction with NSArray as follows:
NSArray * myArray;
NSNumber * myFloatObj = [NSNumber numberWithFloat:3.14];
myArray = [NSArray arrayWithObjects:myFloatObj, nil];
You can then get the original float value from the first NSNumber of the array:
NSNumber * theNumber = [myArray objectAtIndex:0];
float theFloat = [theNumber floatValue];
Alternatively, you can turn this into a one-liner:
float theFloat = [[myArray objectAtIndex:0] floatValue];
Primitive types can't be included in a NSArray, which is only for objects. For numbers, use NSNumber to wrap your floats.
NSNumber *n1 = [NSNumber numberWithFloat:1.2f];
NSNumber *n2 = [NSNumber numberWithFloat:1.4f];
NSArray *array = [NSArray arrayWithObjects:n1, n2, nil];

How to assign integer from NSMutableArray

I would like to assign NSInteger by using NSMutableArray is there any way to solve this?
It is not working on simulator and cut off when run the application.
NSInteger Section;
NSMutableArray dataSourceSection;
Section = (NSInteger)[dataSourceSection objectAtIndex:2];
Thank you.
A NSMutableArray only stores objects. NSInteger is not an object, but a primitive data type. There is a class NSNumber, however, that can be used instead to store numeric values inside objects. Here's one example.
NSNumber *five = [NSNumber numberWithInteger:5];
NSMutableArray *numbers = [NSMutableArray array];
[numbers addObject:five];
To get the object back and retrieve the integer value use,
NSNumber *firstNumber = [numbers objectAtIndex:0];
NSInteger valueOfFirstNumber = [firstNumber integerValue];
you can't pull an NSInteger out of an NSMutableArray, basically because you can't put anything rather than objects in. in your case NSNumber would be the way to put numbers in NSMutablearray. if you do so you can easily get hold of your object, which is an NSNumber, and convert it to a NSInteger by:
NSMutableArray *array = [[NSMutableArray alloc] init];
// populate the array with NSNumbers
NSInteger number = [[array objectAtIndex:2] intValue];