NSManagedObject custom class & for loop
NSManagedObject custom class & for loop
- Subject: NSManagedObject custom class & for loop
- From: Steven Hamilton <email@hidden>
- Date: Mon, 29 Sep 2008 21:21:39 +1000
Hi folks,
In my core data app I have an Account entity. I've subclassed this as
I want to add a method that calculates the Account's balance by doing
a fetch on "Transaction" entities and tallying up the amounts. I've
created a custom class in xcode and added the following code in
Account.m
#import "Account.h"
@implementation Account
@dynamic name;
@dynamic envelopeVisible;
@dynamic accounts;
@dynamic out;
@dynamic type;
@dynamic in;
@dynamic envelope;
//custom stuff
- (NSNumber *)balance
{
// we fetch all the transactions our account is related to and tally
up the balance
NSNumber *balance;
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Transaction" inManagedObjectContext:moc];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"toAccount
= %@", self];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *transactions = [moc executeFetchRequest:request error:&error];
if (transactions == nil){
NSLog(@"Account balance fetch returned nil");
}
NSLog(@"Account balance for %@ counted %d transactions",[self name],
[transactions count]);
//iterate through transactions and tally balance
NSManagedObject *transaction;
for (transaction in transactions){
NSNumber *amount = [entity amount];
balance = [NSNumber numberWithFloat:[balance floatValue] - [amount
floatValue]];
}
return balance;
}
@end
The problem is when the code hits the for (transaction in
transactions) loop it dumps to GDB with an obj_msgsend error and I
can't see why. Am I trying to do something that a custom class
shouldn't be able to do? If I comment out the loop it runs fine.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden