Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
Hi

I've been looking a solution to my problem. I red this document:
http://developer.apple.com/document...l/KeyValueCoding/Concepts/ArrayOperators.html

- but I still have no glue how to do this.

I have an Array, which is bind to Person class.
Person class has 2 values: "Name" and "Total"

I want to sum "Total" column where name equals someName.
I mean I don't want to sum whole column but only the rows with certain "Name" value.

I probably have to use loop function but I'm not sure how to do this.

I would be very thanful if anyone can help me with this.

Helikutt
 

Bakerman

macrumors member
Jan 31, 2005
62
7
Sweden
Use filteredArrayUsingPredicate: to filter out only persons with the name you are looking for. Then use valueForKeyPath:mad:"@sum.Total" on the resulting array to get the value you are looking for.

BTW, it is standard practice to use lowercase for the initial letter in property names.
 

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
Thanx for reply, I will try this out.

The property names are actually starting with lowercase in my class file.
I don't know why I typed them uppercase here, I'll be more careful next time.

Helikutt
 

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
Hi again

Here is the code I figured out so far - I got 2 errors.

Code:
- (double)johnTotal {

	NSString *userName = @"John";
	
	NSPredicate *johnTotalPredicate = [NSPredicate predicateWithFormat:@"person.name like %@", userName];
	NSArray *jTotal = [array filteredArrayUsingPredicate:johnTotalPredicate];

I get an error here saying: 'array' undeclared (first use in this function)
I believe I should declare my Users array here and change the array to Users array - but I'm not sure how to do that.

Code:
	return [jTotal valueForKeyPath:@"@sum.total"];
}

Another error here: incompatible types in return
How should I avoid that.

Code:
- (void)setJohnTotal:(double)aJohnTotal {
	johnTotal = aJohnTotal;
}

Am I close or not?
Again, I'd be very thankful if someone would help me out here.

Helikutt
 

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
I've been reading everything about filteredArrayUsingPredicate: , but I'm still not sure how to use it.

Anyone willing to help
 

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
I'm continuing my monologue here -

I upgraded my little database a bit following tutorials from cocoacast.
I now have a document based cocoa application.
Added save & open funcion and a relationship.
I now have to classes - Person and Total

Person class holds single value "name"
Total class holds 3 values "date", "person" and "total".

I've made some progress - 2 previous errors are gone but I have a new one at the end:
error: incompatible types in assignment

So basically I'm still in the same place.
If anyone notice a problem that causes the error, please let me know.


Problematic code:

Code:
- (double)johnTotal {

	NSString *userName = @"John";
	
	NSPredicate *johnTotalPredicate = [NSPredicate predicateWithFormat:@"person.name like %@", userName];
	NSArray *jTotal = [totals filteredArrayUsingPredicate:johnTotalPredicate];

	return [jTotal valueForKeyPath:@"@sum.total"];
}
 

mysticwhiskey

macrumors newbie
Mar 31, 2008
25
0
I'm continuing my monologue here -

I upgraded my little database a bit following tutorials from cocoacast.
I now have a document based cocoa application.
Added save & open funcion and a relationship.
I now have to classes - Person and Total

Person class holds single value "name"
Total class holds 3 values "date", "person" and "total".

I've made some progress - 2 previous errors are gone but I have a new one at the end:
error: incompatible types in assignment

So basically I'm still in the same place.
If anyone notice a problem that causes the error, please let me know.


Problematic code:

Code:
- (double)johnTotal {

	NSString *userName = @"John";
	
	NSPredicate *johnTotalPredicate = [NSPredicate predicateWithFormat:@"person.name like %@", userName];
	NSArray *jTotal = [totals filteredArrayUsingPredicate:johnTotalPredicate];

	return [jTotal valueForKeyPath:@"@sum.total"];
}

Hi,

I think it might be the last line that's causing the problem:

Code:
return [jTotal valueForKeyPath:@"@sum.total"];

since the valueForKeyPath method returns an object, but your method is defined as returning a double. Maybe the following will make it work:

Code:
return [[jTotal valueForKeyPath:@"@sum.total"] doubleValue];

Edit: if this doesn't work, it might be useful to split the last line into two so you can see if the valueForKeyPath method is returning a non-nil number object:

Code:
NSNumber* myTotal = [jTotal valueForKeyPath:@"@sum.total"];
if (myTotal != nil)
    return [myTotal doubleValue];
else
    return 0;
 

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
thanx mysticwhiskey, it works.

Now I run into another problem. How to make it to update the johnTotal when I insert new entry to the table. At the moment it calculates when I save and reopen the document.

In my total class I have a value that is calculated and is updated using

[Total setKeys:[NSArray arrayWithObjects:mad:"value", @"tax", nil]
triggerChangeNotificationsForDependentKey:mad:"total"];

I'm trying to figure this one out myself.

Thanx again Bakerman and mysticwhiskey
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Worst case you do it without bindings, i.e. load the table manually and use the delegate to check when the selection is changed.
 

Helikutt

macrumors newbie
Original poster
Apr 19, 2008
8
0
Hi again

I just wanted to thank you all who helped me with this issue.

I managed to solve the update problem using
willChangeValueForKey: and didChangeValueForKey:

Everything works perfectly.

Thanx again
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.