Convert NSMutableArray into integer - iphone

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]];
}

Related

Storing a double array as NSNumber

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).

How to store integer value into an NSMutableArray in iphone

I have NSMutableArray having values 10,15,26,28. I want to store them into another array as an integer form how do i store them.
Thanks :)
here how to add
[array addObject:[NSNumber numberWithInt:10]];
you can get the integer value like
//assuming array has nsnumber/nsstring objects.
[[array objectAtIndex:index] intValue];
Here's an example of how to do this:
int yourInt = 5;
[myMutableArray addObject:[NSNumber numberWithInt:yourInt]];
You can't store C types in a NSMutableArray, you can only store objects.
Create NSNumber's from your int values with [NSNumber numberWithInteger:10];...
You can then get the int value back with [aNSNumberObject intValue];
If you want to store integers in an array it has to be a C array:
#define C_ARRAY_MAX_SIZE 10
int cTab[C_ARRAY_MAX_SIZE];
int i=0;
for (NSNumber* n in yourMutableArray) {
cTab[i] = [n intValue];
i++;
}

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];

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

How can I access the int values of an object in an NSMutableArray?

I try to access some values in a NSMutableArray I created, but I only get some numbers (address?) if I try to access them.
I was able to initialize an array and can add and change objects with
[myNSMutableArray addObject:[NSNumber numberWithInt:10]]
and
[myNSMutableArray replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:47]
I also can print the value at index [0] with
NSLog(#"%#", [myNSMutableArray objectAtIndex:0]);
and I get 47 as expected.
But how can I access the integer value of the object in the array so I can save it tomyIntValue?
int myIntValue = [[myNSMutableArray objectAtIndex:0] intValue];
Just have a look at the NSNumber class reference