How to store integer value into an NSMutableArray in iphone - 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++;
}

Related

Using a for-in loop with NSInteger?

I have an NSMutableArray populated with NSIntegers. I need to loop through the array. I could do:
// given NSMutableArray *array of NSIntegers
NSUInteger n = [array count];
for (NSInteger i = 0; i < n; i++) {
NSInteger x = [array objectAtIndex:i];
// query SQLite WHERE id = x
}
However, it seems that a for (object in array) loop would be cleaner. iOS 5 does not accept NSIntegers or NSNumbers as objects in for-in loops. Should I loop through the array with NSObjects, casting the NSObject to an NSInteger during each iteration? Is there another way? Or is a for loop like the one above the cleanest solution to this problem?
In Objective-C you can use a for-in loop with NSNumber like this:
NSArray *array = /*NSArray with NSNumber*/;
for (NSNumber *n in array) {
NSLog(#"i: %d", [n intValue]);
}
Check this out.
Mostly, you will not be allowed to have an NSMutableArray of NSUInteger (aka unsigned long) as it's not an objective-c object.
You may use the c style.
NSUInteger array[] = {value1,value2,value3};
int size = sizeof(array)/sizeof(array[0]);
for (int i=0; i<size; i++) {
NSInteger value = array[i];
// do whatever you want
}

How to store integer value into an array in iPhone?

My mutableArray is
NSDictionary *dict;
self.dayArray = [[NSMutableArray alloc] initWithCapacity:[xmlParseArray count]];
for(dict in xmlParseArray)
if([dict objectForKey:#"day"])
[self.dayArray addObject:[dict objectForKey:#"day"]];
NSLog(#"dayArray is : %#",dayArray);
dayArray print : 10,15,26,28....
I want to convert these value into integer format and store it another array. For this I am doing this:
int currentNumber;
for(int i=0; i<[dayArray count]; i++)
{
currentNumber = [((NSNumber*)[dayArray objectAtIndex:i]) intValue];
}
NSLog(#"currentNumber is : %d",currentNumber);
currentNumber print 10,15,26,28 but I want to store it into array having integer format. These are day and I want to show these day into calendar with highlighted red color
Why don't just pass dayArrayDirectly? It is already have NSNumber Objects into it.
try this
[dictionaryArray setObject:[NSNumber numberWithInt:1] forKey:#"ANumberValue"];

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

NSInteger to NSArray iPhone

I have NSInteger variable, for example NSInteger example=1256 and i need an array with elements of this variable.
so first element of array is array[0] = 1
array[1] = 2
array[2] = 5 etc..
what way can i solve it ?
Here's about how I'd do it:
NSUInteger number = 1234567890;
NSMutableArray * numbers = [NSMutableArray array];
while (number > 0) {
NSUInteger lastDigit = number % 10;
[numbers insertObject:[NSNumber numberWithUnsignedInteger:lastDigit] atIndex:0];
number = number / 10;
}
You need to use NSMutableArray to be able to change entries. NSMutableArray can only hold objects, not primitive types like NSInteger. Also, if you are using NSMutableArray, you can't access the elements the same way as with a C array.
[array insertObject:[NSNumber numberWithInteger:2] atIndex:1];
You can convert your integer to a char* then iterate through it casting each character back to an int and adding it to a C array or, as Steven says, an NSArray of NSNumbers.

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