How to add array values using NSMutableArray in iPHone? - 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"];

Related

search the values in array in iPhone sdk

I have the array like:
(
{
id=1;
Title="AAAA";
period_id=1;
},
{
id=2;
Title="BBBB";
period_id=2;
},
{
id=3;
Title="CCCC";
period_id=2;
},
{
id=4;
Title="DDDD";
period_id=2;
},
{
id=5;
Title="EEEE";
period_id=3;
},
)
Question: How can i know that Period_id=2 is multiple times in the array?
Help me solve this.
Thank you,
There are lots of ways to do so, Some of them are here ..
A:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"period_id == %#", #"2"];
NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
NSLog(#"%d", [newArray count]);
B:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (id obj in array)
{
if([obj[#"period_id"] isEqualToString:#"2"]){
[newArray addObject:obj];
}
}
NSLog(#"%d", [newArray count]);
C:
NSArray *allIds = [array valueForKey:#"period_id"];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:allIds];
for (id item in set)
{
NSLog(#"period_id=%#, Count=%d", item,[set countForObject:item]);
}
D:
NSArray *allIds = [array valueForKey:#"period_id"];
__block NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSString *valueToCheck = #"2";
[allIds enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isEqualToString:valueToCheck])
[newArray addObject:obj];
}];
NSLog(#"%d", [newArray count]);
E:
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *newArray = [array objectsAtIndexes:indexes];
NSLog(#"%d", [newArray count]);
try like this,
NSIndexSet *indices = [questionSections indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *filtered = [questionSections objectsAtIndexes:indices];
NSLog(#"duplictae:%d\n %#",[indices count],filtered);
O/P:-
duplicate: 3
(
{
name = bbbb;
"period_id" = 2;
},
{
name = ccccc;
"period_id" = 2;
},
{
name = ddddd;
"period_id" = 2;
}
)
if the array is sorted, as it seems at your case, just check if the next item has the same value as this one
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == array[i + 1].id) {
// Duplicate
}
}
if you just want to know about id = 2
int idCount = 0;
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idCount++;
}
}
if you also want to know the location
int idCount = 0;
int idarr[array.size()];
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idarr[idCount++] = i;
}
}
I think this is a JSON response from what I gather. Yes you can get the period_id. Add all the period_id's in an NSMutableArray.
Then simply search for the period_id from within this array for the values of the period_id to be same . You will get the index on which the period_id's are same.
NSSet *uniqueElements = [NSSet setWithArray:myArray];
for(id element in uniqueElements) {
// iterate here
}
You could also use NSPredicate to check duplicate.
Try this Example:
NSPredicate *testPredicate = [NSPredicate predicateWithFormat:#"period_id.intValue == %d",value];
NSMutableArray *data = [[NSMutableArray alloc] init];
NSArray *testArray= [yourArray filteredArrayUsingPredicate:testPredicate];
NSLog(#"duplicate:%d",[testArray count]);

How to sort an array with alphanumeric values?

I have an array which contains strings like frame_10#3x.png , frame_5#3x.png,frame_19#3x.png etc.
So I want to sort this array according to the number after the underscore i.e. the correct sequence will be frame_5#3x.png,frame_10#3x.png,frame_19#3x.png.
I tried to use the following method but no result:
NSInteger firstNumSort(id str1, id str2, void *context) {
int num1 = [str1 integerValue];
int num2 = [str2 integerValue];
if (num1 < num2)
return NSOrderedAscending;
else if (num1 > num2)
return NSOrderedDescending;
return NSOrderedSame;
}
Please suggest how to do this sorting for array.
NSArray *sry_img = [[NSArray alloc] initWithObjects:#"frame_18#3x.png",#"frame_17#3x.png",#"frame_1222#3x.png",#"frame_10#3x.png",#"frame_3#3x.png",#"frame_4#3x.png",#"frame_4#3x.png",#"frame_1#3x.png",#"frame_4#3x.png",#"frame_4#3x.png",nil];
NSArray *sortedStrings = [sry_img sortedArrayUsingSelector:#selector(localizedStandardCompare:)];
NSLog(#"%#",sortedStrings);
Enjy .......
But
localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
you want to do something like:
NSArray *components1 = [str1 componentsSeparatedByString:#"_"];
NSArray *components2 = [str2 componentsSeparatedByString:#"_"];
NSString *number1String = [components1 objectAtIndex:([components1 count] - 1])];
NSString *number2String = [components2 objectAtIndex:([components2 count] - 1])];
return [number1String compare:number2String];
I am not sure if my solution is the best possible approach but it can solve your problem for the time being :) .
1) First I have written a function to get the numbers before # character in your string and then I implemented simple SELECTION SORT algo to sort the array using this functions.
- (NSString*)getSubStringForString:(NSString*)value {
// First we will cut the frame_ string
NSMutableString *trimmedString = [NSMutableString stringWithString:[value substringWithRange:NSMakeRange(6, [value length]-6)]];
// New String to contain the numbers
NSMutableString *newString = [[NSMutableString alloc] init];
for (int i = 0; i < [trimmedString length] ; i++) {
NSString *singleChar = [trimmedString substringWithRange:NSMakeRange(i, 1)];
if (![singleChar isEqualToString:#"#"]) {
[newString appendString:singleChar];
} else {
break;
}
}
return newString;
}
This is the selection Implementation of the algo for sorting. The main logic is in the for loop. You can copy the code in viewDidLoad method to test.
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"frame_10#3x.png",#"frame_5#3x.png",
#"frame_3#3x.png", #"frame_19#3x.png",
nil];
NSLog(#"Values before Sort: %#", array);
int iPos;
int iMin;
for (iPos = 0; iPos < [array count]; iPos++)
{
iMin = iPos;
for (int i = iPos+1; i < [array count]; i++)
{
if ([[self getSubStringForString:[array objectAtIndex:i]] intValue] >
[[self getSubStringForString:[array objectAtIndex:iMin]] intValue]) {
iMin = i;
}
}
if ( iMin != iPos )
{
NSString *tempValue = [array objectAtIndex:iPos];
[array replaceObjectAtIndex:iPos withObject:[array objectAtIndex:iMin]];
[array replaceObjectAtIndex:iMin withObject:tempValue];
}
}
NSLog(#"Sorted Values: %#", array);
I hope that it can atleast keep you going. :)
You can try this-
NSString *str1 = [[[[str1 componentsSeparatedByString:#"frame_"] objectAtIndex:1] componentsSeparatedByString:#"#3x.png"] objectAtIndex:0];
int num1 = [str1 integerValue];

How to get index in an NSArray?

NSMutableArray*array = [[NSMutableArray alloc]init];
NSArray*Somearray = [NSArray arrayWithObjects:1st Object,2ndObject,3rd Object,4th object,5th Object,nil];
In the above array 1st Object,2ndObject,3rd Object,4th object,5th Object having val,content,conclusion in each index.
for(int i=0;i<[Somearray count];i++)
{
______________
Here the code is there to give each index ,that is having val,content,conclusion ..
After that val,content,conclusion in each index will be add to Dict..
____________
NSDictionary *Dict = [NSDictionary dictionaryWithObjectsAndKeys:val,#"val",content,#"content",conclusion,#"conclusion",nil];
//Each time adding dictionary into array;
[array addObject:Dict];
}
The above Dictionary is in for loop and the keyvalue pairs will be add 5 times(Somearray Count).Now array is having in
array = [{val="1.1 this is first one",content="This is the content of 0th index",conclusion="this is the conclusion of 0th index"},{val="1.2 this is first one",content="This is the content of 1st index",conclusion="this is the conclusion of 1st index"},____,____,______,{val="1.5 this is first one",content="This is the content of 4th index",conclusion="this is the conclusion of 4th index"},nil];
Now i am having NSString*string = #"1.5";
Now i need the index where val is having 1.5 in it.How to send the str in to array to find the the index.
Can anyone share the code please.
Thanks in advance.
Use method indexOfObject
int inx= [array indexOfObject:#"1.5"];
For Find index particular key value.
int inx;
for (int i=0; i<[array count]; i++) {
if ([[[array objectAtIndex:i] allKeys] containsObject:#"val"]) {
inx=i;
break;
}
}
The method you are looking for is -[NSArray indexOfObjectPassingTest:]. You would use it like this:
NSUInteger i = [array indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[id objectForKey:#"val"] rangeOfString:#"1.5"].location != NSNotFound;
}];
If you just want to check that val starts with "1.5" you would use hasPrefix: instead.
Try this -
NSArray *valArray = [array valueForKey:#"val"];
int index = [valArray indexOfObject:#"1.5"];
Appended answer given by Mandeep, to show you the magic of key value coding ;)
NSUInteger idx = UINT_MAX;
NSCharacterSet* spaceSet = [NSCharacterSet whitespaceCharacterSet];
for(int i=0,i_l=[Yourarray count];i<i_l;i++) {
NSString* s_prime = [[Yourarray objectAtIndex:i] valueForKey:#"val"];
if ([s_prime length] < 4) {
continue;
}
NSString *subString = [[s_prime substringToIndex:4] stringByTrimmingCharactersInSet:spaceSet];
// NSLog(#"index %#",s);
if ([subString isEqualToString:secretNumber]){
idx = i;
break;
}
}
if (idx != UINT_MAX) {
// NSLog(#"Found at index: %d",idx);
} else {
// NSLog(#"Not found");
}

get values form NSMutableArray from end to start

I want to take the values from NSMutableArray but want to read from last index to 1st index
thank you
for (id someObject in [someArray reverseObjectEnumerator])
{
//do your thing
}
2 other options:
Simple for-loop (surely not recommended):
for (int i = [array count]-1; i >= 0; ++i)
id value = [array objectAtIndex: i];
Block-based enumeration:
[array enumerateObjectsWithOptions: NSEnumerationReverse
usingBlock: ^(id obj, NSUInteger idx, BOOL *stop){
//do something
}];
Mark's answer is useful, but this form may be useful when you want to mutate the array:
while ([arr count]) {
id obj = [arr lastObject];
// use obj
[arr removeLastObject];
}

How to add numbers in a mutable array in iPhone?

I am new to iPhone development. I want a Nsmutable array to hold numbers from 1 to 100. How can I do it? How can I implement in a for loop? Is there any other way to hold numbers in array in iPhone?
You can only add NSObject subclasses in Cocoa containers. In your case, you will have to wrap your integers in NSNumber objects:
NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
[array addObject:[NSNumber numberWithInt:i]];
}
To extract the values:
int firstValue = [[array objectAtIndex:0] intValue];
Use an NSNumber object:
[NSNumber numberWithInt:1];
The short hand solution
NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
[array addObject:#(i)];
}
int intValue = 10;
NSNumber *numberObj = #(intValue);