Storing a double array as NSNumber - iphone

I have a variable double * data = malloc(sizeof(double)) in objectiveC;
I am using this variable as an double array like data[] to store some data. Now I want to add this data variable (which is an double* array) as an object NSNumber in iOS. Any idea how I can turn it into iOS object likeNSNumber`?

You can use NSData to wrap an arbitrary byte buffer into an Objective-C object.
Use dataWithBytes:length: to create an NSData object from your double array, and bytes: or getBytes:length: to retrieve the data bytes back from the NSData object.

You cannot turn an array of primitives into one NSNumber. This does not make any sense.
You can, however, turn an array of doubles into an array of NSNumbers. Iterate through your double* array and add each number to an NSMutableArray as NSNumber using its class method numberWithDouble:.

Based on Mundi's answer, try this:
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < lengthOfDoublearray; i++) { // as premitive DataType array needs predefined length
[array addObject:[NSNumber numberWithDouble:data[i]]];
}
Here data is array of double (that you used).

Related

Convert NSMutableArray into integer

i am working on xml parsing and getting three item numbers from the server 1
2 3, i have taken the three numbers into nsmutable array and assign them in delegate value mutable array, now i want to pass one number at a time into another function to get response from the server, so someone please let me know that how to convert NSMutable array value into integer while parsing into another function.
do like this
for(int i=0;i<[yourArrayFromXmlParsing count];i++)
{
int a=[[yourArrayFromXmlParsing objectAtIndexPath:i] intValue];
[obj function:a];
}
If u have added each number into the array u can access them by using the index of the array.
[[myArray objectAtIndex:i] intValue];
Assuming inside your array, you have NSNumber objects:
for (NSNumber *aNumber in yourArrayFromXmlParsing) {
resultFromServer = [self fooMethod:[aNumber intValue]];
}

Convert NSString to Integer in objective - c

I have an array that stores each digit of a phone number as a string. I then pass the phone number digit string as an argument for objectAtIndex: method of NSArray like this: [myArray objectAtIndex: [myString intValue]]; The compiler says I need to cast the String but I am already doing that. What is wrong?
Update:
Here's my actual line of code:
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[charHolder objectAtIndex:[[phoneNumDigits objectAtIndex:i]intValue]]];
This is where the error is, phoneNumDigits is an array of each digit of the phone number, charHolder is the array holding the array of letters associated with each digit.
Your question is a little confusing to me. Here is how I understand what you want to do:
You have an NSArray named phoneNumDigits. This array contains a few NSString objects. Each string is something like #"1" or #"4" and represents a single digit of a phone number.
Now you want to convert each of those digit strings to int or NSInteger and want to store these integers in another array.
If I understood you correctly, here is my answer:
You cannot exactly do what you want, because you can't put a simple data type like an int or a float into an NSArray.
That's why there is the wrapper class NSNumber. You can package a simple int in an NSNumber and store this NSNumber in an NSArray.
So to get your string digits from the phoneNumDigits into the tmp array you could use this code:
for (NSString *digitAsString in phoneNumDigits)
{
NSNumber *digitAsNumber = [NSNumber numberWithInt:[digitAsString intValue]];
[tmp addObject:digitAsNumber];
}
To get the ints out of the tmp-NSArray you would use
int digit = [[tmp objectAtIndex:idx] intValue];
I hope this helps, but I'm not sure I understand what you want to do here. I could be completely missing the point. Maybe you could share some more code.
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[charHolder objectAtIndex:[[phoneNumDigits objectAtIndex:i]intValue]]];
objectAtIndex returns a generic object (id). It has no idea that the object in the array is a string as it could be anything. So you need to cast it. Or to increase readabilty, create variables for them. Eg:
int phoneNumberDigit = [phoneNumDigits objectAtIndex:i];
NSArray *chars = [charHolder objectAtIndex:phoneNumberDigit];
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:chars];

float* array to NSArray, iOS

I have a float pointer array and I would like to convert it to an NSArray.
Is there a better way to do it than to iterate through the float* and add each entry to the NSArray?
I have:
float* data = new float[elements];
fill up data from binary ifstream
I want to avoid doing something like:
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:elements];
for (int i=0;i<elements;i++)
{
[mutableArray addObject:[NSNumber numberWithFloat:data[i]]];
}
NSArray *array = [NSArray arrayWithArray:array];
Is there some convenience / more efficient method to copy a large chunk of floats into an NSArray?
Regards,
Owen
You’ve got two problems: first, you can’t store a float in an NSArray, since NSArrays will only hold Objective-C objects. You’ll need to wrap then in an object, probably NSNumber or NSValue.
As to your original question, since you have to create the objects anyway, there isn’t a better method. I’d recommend the for loop:
for (int i = 0; i < elements; i++) {
NSNumber *number = [NSNumber numberWithFloat:floatArray[i]];
[myArray addObject:number];
}
Keep in mind that number will be autoreleased. If you’re dealing with a lot of numbers, that can get out of hand pretty quickly with memory management, so you might do this instead:
for (int i = 0; i < elements; i++) {
NSNumber *number = [[NSNumber alloc] initWithFloat:floatArray[i]];
[myArray addObject:number];
[number release];
}

Array Allocation in Objective C++

I want to declare an array of numbers that is flexible in size in an obj C file
I am doing this:
long * arr = NULL;
a[0] = 0;
but this is giving bad_excess error
Can anyone help me out
also
long *arr = malloc(sizeof(long));
doesn't seem to help either
Why don't you do something like this,
NSMutableArray *numArray = [[NSMutableArray alloc] init];
This creates a array of objects. You can add any number of objects to it. Here you want to add long values. So use the following code,
[numArray addObject:[NSNumber numberWithLong:37];
[numArray addObject:[NSNumber numberWithLong:45];
[numArray addObject:[NSNumber numberWithLong:12];
NSMutableArray is flexible in size as you expected. You can add any number of objects.
If you want to get the number, you can do like the following,
long num1 = [[numArray objectAtIndex:0] longValue];
long num2 = [[numArray objectAtIndex:1] longValue];
You can also do as specified in the following link,
How to declare an array of floats as a class variable in Objective-C when the dimension is undefined at the class instantiation time?

Problem retrieving data from array iphone?

I declared a nsmutablearray and assign some object but when retrieving object it gives memory location.Here is the code.
NSMutableArray *arrCondiNum = [[NSMutableArray alloc] init];
[arrCondiNum addObject:#"2"];
[arrCondiNum addObject:#"4"];
[arrCondiNum addObject:#"6"];
[arrCondiNum addObject:#"8"];
for(int i = 0;i<[arrCondiNum count];i++){
NSLog(#"Array number %d",[arrCondiNum objectAtIndex:i]);
}
Output it gives
Array number 20820
Array number 20836
Array number 20852
Array number 20868
You add string in your array, then to display it, you have to use %#:
NSLog(#"Array number %#",[arrCondiNum objectAtIndex:i]);
In your code (%d), you display the address of the object.
%# will display the description of the ObjC object (return of -descriptionWithLocale: or -description)
Note that in case you want an array of numbers, use NSNumber instead:
[array addObject:[NSNumber numberWithInt:1]];
You still have to use the %# format specifier though (as NSNumber is a class-type) or retrieve the integer value using -intValue.
Try [[arrCondiNum objectAtIndex:i] floatValue] instead. NSArrays are keeping objects (pointers to objects to be more precise), not values. So the array will return the string object you created earlier. By sending this object the "floatValue" message, it will return its content represented as float value. You could use intValue as well to receive an integer.
just write like this..
NSLog(#"Array number %#",[arrCondiNum objectAtIndex:i]);
u have to print some text..so