 |
 |
Add Column Values
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
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?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
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?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Status:
Offline
|
|
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];
...
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2003
Location: Evansville, IN
Status:
Offline
|
|
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. 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
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];
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|