How to add items from NSArray :

RemoRoid

I want to make method "additionMulti" that take n paramiters and add them in sum

My Code :

-(void) additionMulti:(NSArray *)data
{
    int sum=0;
    int xx;
    for(xx=0; xx < [data count]; xx++)
    {
        sum =sum + [data  objectAtIndex:xx];
    }

     NSLog(@"The result of adding %@ is : %i",data,sum);
}
Marco Pace

If you need to calculate the sum from an array of objects, try with this:

- (void)additionMulti:(NSArray *)data {
    int sum = 0;
    int xx;

    for(xx=0; xx<[data count]; xx++) {
        sum = sum + [[data objectAtIndex:xx] intValue];
    }

    NSLog(@"The result of adding %@ is : %i",data,sum);
}

In order to let this code works, your array must be an array of NSNumber or NSString (so you can take the intValue from each value in the array).

In case you need to add an array to an already existing array, I suggest you to follow Julian' solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add object to NSArray with key from database?

Accessing Items in NSArray from PFObject

How to add items returned from map function

How to add items from an SQL query to a TListView

How to add action items from SherlockFragment

How to add to SortedSet items from an Array?

How to add items to a string array from a dropdownlist

How To Add Items From a ComboBox to a List Collection

How to add items from combo box into array

How to add and remove class from list items?

How to add items from listbox to listview?

How to add items from multiple QStringLists to one?

How to add items to the map from coordinates in the database?

How to add items from a listbox to another listbox?

how to add and remove items from an arraylist object

How to add NSArray object which is located inside NSMutableDictionary and NSArray property?

How to search From Nsarray in iOS?

How to populate a UITableView from NSArray?

How to select an UIImgeView from NSArray

How can I add values to NSArray of different class A from Class B

How to add nil value to a NSArray in swift?

How to Add Multiple NSDictionaries to NSArray in objective c?

How to add/remove items from a list from a different form?

How to create a new NSArray with the last 30 objects from another NSArray?

How to see if object is contained in an embedded NSArray, then grab the other items in the set

How to correctly add and remove specific items from an array within state?

How to add multiple items from an array to another array

How to add items from a list to an AutoCAD file in C#?

How to add items to a listbox from another form in C#