Re: Array controllers in code?
Re: Array controllers in code?
- Subject: Re: Array controllers in code?
- From: Michael Swan <email@hidden>
- Date: Fri, 23 Jul 2010 04:44:42 -0400
Amy,
A few things with what you are trying to do. First off, using IBOutlet for array controllers is totally correct, basically any element you add in IB you should access through an IBOutlet. As for the calculation of the a value based on other values in Core Data you have a few problems. The first one being that you are outside MVC with your approach as you are generating model data in a view controller.
Judging by the code below it seems as though cost is already an attribute in your Core Data model, if this os not the case for some reason then you can add it as a transient attribute as long as you don't need to search by the value. Once the cost attribute is in your model create a custom subclass for that attribute (Xcode will take care of all the default code for you). You need to make one change to the .h file for your new class, in the @property for cost you need to replace the word 'retain' with the word 'readonly'. Then you just need to add this to the .m file and you should be able to bind to the cost attribute the same way you have bound to other attributes (I suggest makeing any element that shows the cost non-editable)
+(NSSet *)keyPathsForValuesAffectingTotal {
return [NSSet setWithObjects:@"UOMcost", @"purchaseUOM", nil];
}
-(NSDecimalNumber *)cost {
if ( [self.UOMcost compare:[NSDecimalNumber zero] != NSOrderedSame && [self.purchaseUOM compare:[NSDecimalNumber zero] != NSOrderedSame) {
return [self.UOMcost decimalNumberByDividingBy: self.purchaseUOM];
}
else {
return [NSDecimalNumber zero];
}
}
Feel free to check out my tutorials at http://themikeswan.wordpress.com/ for more Core Data details.
FYI, this is called a derived property as it is derived from other properties of of the entity.
Mike Swan
ETCP Certified Entertainment Electrician
http://www.michaelsswan.com
"Every experience in your life is an opportunity to learn something new and to make a change for your ultimate benefit."
On Jul 22, 2010, at 3:02 PM, email@hidden wrote:
> Fantastic, Thank You, got that bit working, just for reference I've
> got the following:
>
> @interface ishop_AppDelegate : NSObject
> {
> IBOutlet NSWindow *window;
> IBOutlet NSArrayController *Products;
>
> NSPersistentStoreCoordinator *persistentStoreCoordinator;
> NSManagedObjectModel *managedObjectModel;
> NSManagedObjectContext *managedObjectContext;
> }
>
>
> /** Calculate Product Cost */
> - (IBAction)calcCost:(id)sender;
> {
> NSObject *Product;
> double *price, *uom, *cost;
> Product = [[Products selectedObjects] objectAtIndex:0];
> price = [Product valueForKey:@"UOMcost"];
> uom = [Product valueForKey:@"purchaseUOM"];
> cost = uom/price;
> [Product setValue:cost forKey:@"cost"];
>
> }
>
> although the actual calculation won't work, not sure why, example
> values are price=8.8, uom=25, getting an error:invalid operands to
> binary?
>
> Thank you though, this will help me so much with this project,
_______________________________________________
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