Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-15-2009, 10:21 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default Sum of Objects in an Array

How do you get the total sum of the contents of an array? I have an array filled with integers and here is my code. Instead of objectAtIndex I need something like SumofObjects:1,2,3,4,5; is this possible? Any help would be great thanks!

NSString *title = [delegate.titlearray objectAtIndex:0];
cell.Label.text = title;
starwarsdevwookie59 is offline   Reply With Quote
Old 07-15-2009, 11:34 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Fort Lauderdale, Florida
Posts: 67
Default

Quote:
Originally Posted by starwarsdevwookie59 View Post
How do you get the total sum of the contents of an array? I have an array filled with integers and here is my code. Instead of objectAtIndex I need something like SumofObjects:1,2,3,4,5; is this possible? Any help would be great thanks!

NSString *title = [delegate.titlearray objectAtIndex:0];
cell.Label.text = title;
I am not at my mac right now to test this but it should work.
Code:
int sum=0;
for(int x=0; x < [array count]; x++)
{
    sum += [array objectAtIndex:x];
}
Ins3rtNam3H3r3 is offline   Reply With Quote
Old 07-16-2009, 10:42 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

Quote:
Originally Posted by Ins3rtNam3H3r3 View Post
I am not at my mac right now to test this but it should work.
Code:
int sum=0;
for(int x=0; x < [array count]; x++)
{
    sum += [array objectAtIndex:x];
}
Thanks! The code dosent give any errors but it gives two warnings. One in your code and the other when i try to set the cells text as "sum". They are "makes integer from a pointer without a cast" You know what that means?
starwarsdevwookie59 is offline   Reply With Quote
Old 07-16-2009, 11:35 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Assuming your "array" has NSNumber objects..

Code:
int sum=0;
for(int x=0; x < [array count]; x++)
{
    sum += [[array objectAtIndex:x] intValue];
}

//to set the text
label.text = [NSString stringWithFormat:@"%d", sum];
smithdale87 is online now   Reply With Quote
Old 07-16-2009, 04:06 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

Quote:
Originally Posted by smithdale87 View Post
Assuming your "array" has NSNumber objects..

Code:
int sum=0;
for(int x=0; x < [array count]; x++)
{
    sum += [[array objectAtIndex:x] intValue];
}

//to set the text
label.text = [NSString stringWithFormat:@"%d", sum];
Um I'm pretty sure my array dosent have NSNumber objects. It's just a normal NSMutable array initwithObjects:@"1",@"2","3",nil.
How do I make it into that? Thanks!
starwarsdevwookie59 is offline   Reply With Quote
Old 07-16-2009, 04:30 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

ok, so it has strings..

the same code should still work
smithdale87 is online now   Reply With Quote
Old 07-16-2009, 04:46 PM   #7 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 12
Default

If you don't need an array of a variable size, you could just use an standard array.

int array[3];

ints use a lot less memory than NSStrings + NSMutableArrays, and I don't think that you need all the features that they bring.
Pelaez is offline   Reply With Quote
Old 07-16-2009, 08:24 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

Quote:
Originally Posted by Pelaez View Post
If you don't need an array of a variable size, you could just use an standard array.

int array[3];

ints use a lot less memory than NSStrings + NSMutableArrays, and I don't think that you need all the features that they bring.
The arrays need to be editable. add/delete/modify etc... can that handle things things? would the app crash if i loaded say 150 NSMutables at launch?
starwarsdevwookie59 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, objects, sum, total

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 248
21 members and 227 guests
ADY, beleg_1998, Dani77, diyora, FAED, fredidf, F_Bryant, iDifferent, JamesCahall, JasonR, mer10, Oral B, prchn4christ, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:09 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0