How to store integer value into an array in iPhone? - 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"];

Related

compare Integer values using array

I have some integer values inside NSMutableArray. I have added a UITextField and a UIButton. If a number is entered inside textfield and clicking on the button does a comparison. If number entered matches, I need to show NSLog. But it's not working.
code:
arr = [[NSMutableArray alloc]init];
[arr addObject:[NSNumber numberWithInteger:1]];
Button click:
-(void)click:(id)sender{
if (text.text == [arr objectAtIndex:0]){
NSLog(#"values matched");
}
}
Try this
-(void)click:(id)sender{
NSString *str = [NSString alloc]initWithFormat:#"%d",[arr objectAtIndex:0]];
if([text.text isEqualToString: str]){
NSLog(#"values matched");
}
}
I am assuming the array contains NSNumber objects; and if so convert the textfield content to an NSNumber object and use [NSArray indexOfObject] to find it in the array:
- (void)click:(id)sender{
NSNumber *num = [NSNumber numberWithInt:[text.text intValue]];
NSUInteger index = [arr indexOfObject:num];
if (index != NSNotFound) {
NSLog(#"values matched");
}
}

How to iterate textfield From Two Different NSMUTABLEARRAY But index are same

How to calculate and pick the value from two diffrent array on a same loop?
something like that
eg:
for(UITextField *field in textFieldArray && textFieldArray2)
i have to subtract the value and i have 2 Array and i want to subtract the value of two textfield from different array but same index...
My code is
int result = 0;
for(UITextField *field in textFieldArray) // will iterate all UITextField which was added
{
result += [field.text intValue]; //or floatValue
}
NSLog(#"the result is %d", result); //here you have sum of all the text fields' text
i want to subtract this but both text fields are on different Array but have same index...
int y12 = ([Txt_New_Actual.text intValue]);
float c116 = y12-([Txt_New_Estimated.text floatValue]);
Txt_New_ONU.text = [[NSString alloc] initWithFormat:#"%.2f",c116];
Get the index of the current object in your fast enumeration loop, then use indexOfObject: to get the index and use that with objectAtIndex: on your second array.
Something like this:
int result = 0;
for(UITextField *field in textFieldArray)
{
int indexOfObject = [textFieldArray indexOfObject:field];
UITextField *secondValue = (UITextField *)[textFieldArray objectAtIndex:indexOfObject];
result += [secondValue.text intValue];
}
NSLog(#"the result is %d", result);
You should still put some kind of check into that code if the returned value from the 2nd array is valid.
Either use the enumerateWithBlock or have a variable index that you increment in the loop. Access the other array with objectAtIndex:.
Another solution is looping using a for clause instead of foreach, e.g.:
int result = 0;
int count = [textFieldArray count];
for(int i=0; i<count; i++)
{
UITextField field = [textFieldArray objectAtIndex:i];
int y12 = ([field.text intValue]);
field = [theOtherArray objectAtIndex:i];
float c116 = y12-([field.text floatValue]);
}

How to add array values using NSMutableArray in iPHone?

I have the data into the mutable array and the value of array is,
{ "20", "40", "50","60", "70"}.
I have stored the string values into the array.
Now i want to total value of the array. Result is : 240
Thanks!
NSInteger value = 0;
for (String *digit in myArray) {
value += [digit intValue];
}
int total=0;
for(NSString *currentString in myArray){
total +=[currentString intValue];
}
NSLog(#"Sum:%d",total);
This adds all values:
__block NSInteger sum = 0;
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
sum += [obj intValue];
}];
You can do as follows:
int totalSum = 0;
NSmutableArray *arrayData = [[NSmutableArray alloc] init];
[arrayData addObject:#"20"];
[arrayData addObject:#"40"];
[arrayData addObject:#"50"];
[arrayData addObject:#"60"];
[arrayData addObject:#"70"];
for(int i=0; i<[arrayData count];i++)
{
totalSum = totalSum + [[arrayData objectAtIndex:i] intValue];
}
NSLog(#"Total:%d",totalSum);
Please let me know if you have any question.
How about the most elegant solution using key-value Collection aggregators:
NSNumber *sum = [myArray valueForKeyPath:#"#sum.self"];

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

Filter a NSMutableArray index beyond bounds Problem

I am trying to filter a NSMutableArray. Search array for items by certain country. I have tried NSpredicate which works great however I need to re-use the original array which I cannot with NSpredicate. So I am trying the below code.
Q. What is the best way to filter an NSMutableArray keeping the original array intact?
//The following code works
filteredArray = [[NSMutableArray alloc] init];
unsigned int i;
for (i = 0; i < [appDelegate.arrayToBeFiltered count]; i++) {
id session = [appDelegate.ads objectAtIndex: i];
id Country = [[appDelegate.arrayToBeFiltered objectAtIndex: i] TheCountry];
[filteredArray addObject: session];
However when I add the if statement as below I get index beyond bounds
filteredArray = [[NSMutableArray alloc] init];
unsigned int i;
for (i = 0; i < [appDelegate.arrayToBeFiltered count]; i++) {
id session = [appDelegate.ads objectAtIndex: i];
id Country = [[appDelegate.arrayToBeFiltered objectAtIndex: i] TheCountry];
if (Country == #"United States"){
[filteredArray addObject: session];
}
}
Use - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
As a side-note, in the code above you probably mean to do this [Country isEqualToString: #"United States"]
As an extra side note - don't capitalise variables and method names. Just a style thing but capitalisation is usually reserved for Class names