• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSManagedObject custom class & for loop
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: NSManagedObject custom class & for loop
      • From: email@hidden
  • Prev by Date: Mouse events with higher priority then repeating NSTimer
  • Next by Date: Re: Load launchd job immediately, without restart - how?
  • Previous by thread: Re: Mouse events with higher priority then repeating NSTimer
  • Next by thread: Re: NSManagedObject custom class & for loop
  • Index(es):
    • Date
    • Thread