Newbie Problem with KVC compliance

  • I am a newbie to Cocoa programming and am trying to make sense of
    CoreData and bindings. I have run in to a problem. I am trying to set
    a value for a sub-class of ManagedObject: Orders which is mapped to
    an Entity: Orders. I have created and instantiated a class:
    DownloadController, in interface builder, that responds to a a button
    click.
      I want to create an instance of the Orders class and the set value
    for an attribute: message. It compiles OK but when I run it and click
    the button I get the following message: "...<Orders 0x3a9270>
    setValue:forUndefinedKey:]: this class is not key value coding-
    compliant for the key message. "

    Having created this and set the values for the attributes for  I then
    want to add the object to the Orders Array Controller I have created
    in interface builder.
      I suspect I am missing something obvious but have not been able to
    find code examples that do what I am attempting to do. I have
    included code snippets below. Any help will be much appreciated.

    code snippet Orders.h

    #import <CoreData/CoreData.h>

    @interface Orders :  NSManagedObject
    {
    }

    - (void)setMessage:(NSString *)value;

    code snippet Orders.m

    - (void)setMessage:(NSString *)value
    {
        [self willChangeValueForKey: @"message"];
        [self setPrimitiveValue: value forKey: @"message"];
        [self didChangeValueForKey: @"message"];
    }

    code snippet DownloadController.h

    #import <Cocoa/Cocoa.h>

    @interface DownloadController : NSObject
    {
        IBOutlet id Download;

    }
    - (IBAction)Download:(id)sender;

    @end

    code snippet DownloadController.m

    - (IBAction)Download:(id)sender
    {

    Orders  *orders = [[ Orders alloc ]init];
    [orders setValue: @"o" forKey: @"message"];

    }
  • On Thu, 26 Oct 2006 19:22:12 +0200, shaun bear <shaunbear...> said:
    > I am a newbie to Cocoa programming and am trying to make sense of
    > CoreData and bindings. I have run in to a problem. I am trying to set
    > a value for a sub-class of ManagedObject: Orders which is mapped to
    > an Entity: Orders. I have created and instantiated a class:
    > DownloadController, in interface builder, that responds to a a button
    > click.
    > I want to create an instance of the Orders class and the set value
    > for an attribute: message. It compiles OK but when I run it and click
    > the button I get the following message: "...<Orders 0x3a9270>
    > setValue:forUndefinedKey:]: this class is not key value coding-
    > compliant for the key message. "

    Your first impulse should be to believe what you're being told. There is a
    key @"message". You (Orders) are apparently not KVC-compliant for that. To
    see what KVC-compliance entails, read this:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Co
    ncepts/Compliant.html
    >

    Here, "key" is "message". So Orders must implement -message and -setMessage.
    Your quoted code suggests that perhaps you have implemented the latter but
    not the former? m.

    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
  • On Oct 26, 2006, at 10:22 AM, shaun bear wrote:

    > I want to create an instance of the Orders class and the set value
    > for an attribute: message. It compiles OK but when I run it and
    > click the button I get the following message: "...<Orders 0x3a9270>
    > setValue:forUndefinedKey:]: this class is not key value coding-
    > compliant for the key message. "

    You don't want to just alloc/init Managed Objects. They need to be
    connected to a Managed Object Context to be useful. You want to do a
    bit more reading on Core Data first to save yourself some confusion.

    Take a look at NSEntityDescription's
    +insertNewObjectForEntityForName:inManagedObjectContext.

    http://cocoadevcentral.com/articles/000086.php#4

        - Scott
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today