Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Add Column Values

Add Column Values
Thread Tools
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Apr 22, 2003, 08:38 PM
 
Greetings,

How do I add the values of a column? I have a two column table in a drawer, and want to add the values of the second column and then display the result in a label.

Any tips/examples/words of wisdom?

work: maczealots blog: carpeaqua
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Apr 23, 2003, 04:41 AM
 
Presumably your table's data source is storing the values for that column in an array or something similar -- just iterate across the array and add up all the values.
Rick Roe
icons.cx | weblog
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Apr 24, 2003, 03:22 AM
 
Originally posted by Rickster:
Presumably your table's data source is storing the values for that column in an array or something similar -- just iterate across the array and add up all the values.
Code:
int currentValue; int finalGrade; int i; for(i=0; i<= [gradeList count]; i++) { currentValue = [gradeList objectAtIndex:i]; finalGrade = finalGrade + currentValue; } finalGrade = finalGrade / [gradeList count]; NSLog(@"%@",finalGrade);
This tells me that warning: assignment makes integer from pointer without a cast

When the program runs, I get this error:

-[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)


Any help?

work: maczealots blog: carpeaqua
     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
Apr 24, 2003, 10:01 AM
 
Originally posted by tikki:
Code:
int currentValue; int finalGrade; int i; for(i=0; i<= [gradeList count]; i++) { currentValue = [gradeList objectAtIndex:i]; finalGrade = finalGrade + currentValue; } finalGrade = finalGrade / [gradeList count]; NSLog(@"%@",finalGrade);
This tells me that warning: assignment makes integer from pointer without a cast

When the program runs, I get this error:

-[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)


Any help?
The run time error is that you are going beyond the end of your array. For example, if your array has 10 objects, you are requesting the 11th. Remember, arrays start at 0 and go to n-1 when there are n occurences (the 10th object would be when i = 9). So your for loop should be:

Code:
for(i=0; i< [gradeList count]; i++) { ... }
Also, are you storing NSNumbers in your gradeList array. If so, you may need to use the intValue method of NSNumber since your objectAtIndex: method will return a pointer to the NSNumber object. So you might need:

Code:
NSNumber *num; ... num = [gradeList objectAtIndex:i]; currentValue = [num intValue]; ...
     
tikki  (op)
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status: Offline
Reply With Quote
Apr 24, 2003, 04:18 PM
 
Originally posted by lfrog2:


Also, are you storing NSNumbers in your gradeList array. If so, you may need to use the intValue method of NSNumber since your objectAtIndex: method will return a pointer to the NSNumber object. So you might need:
gradeList holds instances of STGrade objects. STGrade just has an NSString and an NSNumber.

I implemented your changes, and now get the following error:

-[STGrade intValue]: selector not recognized

My app window doesnt load then.

I usually only got those when I didnt have an identifier set. I do however have it set.

work: maczealots blog: carpeaqua
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
Apr 24, 2003, 04:46 PM
 
You're new to object oriented programming, eh?

The code [STGrade intValue] says, "Invoke the STGrade class's intValue method." This code will fail unless you have a method in your STGrade class that looks like this:
Code:
+ (int)intvalue { // code here }
What you really want is to send a message to STGrade instances, specifically the ones in your array. The best way to do this is to define a method in your STGrade class such as:
Code:
- (int)getGrade { return [myGrade intValue]; }
Replacing myGrade with the actual name of the NSNumber field in your STGrade class.

That's the best way to do it because the object asking for the grade will not have to know that STGrade has an NSNumber, it just needs to call this method to extract the grade.

Then, in your summing loop, just do something like:
Code:
currentValue = [[gradeList objectAtIndex:i] getGrade];
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 03:26 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2