• 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
Re: Core Data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Core Data


  • Subject: Re: Core Data
  • From: "I. Savant" <email@hidden>
  • Date: Fri, 23 Mar 2007 19:24:42 -0400

On Mar 23, 2007, at 5:49 PM, Keith Penrod wrote:
I have a fairly simple question. I'm doing a Core Data app and I have two columns where I can type in numbers and I'd like the third column (in a table view) to be based on the first two (say a difference or a sum or something). Is there a way to do that with Core Data and IB or do I need to actually code that in somehow?

1: Create your own sublcass of NSManagedObject.
2: To your subclass, add a method with the name of the third attribute that returns the correct type, then flesh it out with the appropriate "this-is-based-on-that" code.
3: Set up proper dependency in your class using the +setKeys:triggerChangeNotificationsForDependentKey: method so that changes to attributes one and two trigger an update on the third, dependent / transient attribute.
4: In your managed object model, set your entity's class to the name of your subclass.
5: Add the third attribute to the entity, but check the "Transient" checkbox and set the appropriate type.
6: Make sure you disallow editing in the table column since you don't have a setter for this property.


The corresponding method in your subclass would look something like this:

- (NSNumber *)totalNumberOfAttendees
{
    int attendees = [[self numberOfAttendees] intValue];
    int guests = [[self numberOfGuests] intValue];

    return [NSNumber numberWithInt:(attendees + guests)];
}

You *could* implement a -setTotalNumberOfAttendees: method, but you'd need a mechanism to figure out how many are attendees and how many are guests, etc. This is highly dependent on your application's logic.

Also, I've left the +setKeys:triggerChangeNotificationsForDependentKey: code as an exercise for the reader.

List, if I've missed missed or misstated anything, feel free to chastise. :-)

--
I.S.



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


References: 
 >Core Data (From: Keith Penrod <email@hidden>)

  • Prev by Date: Re: Getting the last date of the month?
  • Next by Date: Re: Is there a way to re-compile a framework for Intel Mac?
  • Previous by thread: Core Data
  • Next by thread: Progress Sheet on a NSDocument
  • Index(es):
    • Date
    • Thread