Search code examples
cappuccinoobjective-j

How do I make this code KVO compliant?


I'm trying to get a grip on bindings and have set up a CPWindow with a CPTextView in xcode and bound the window and the textview to their respective outlets.

I'm getting a bit stuck however trying to make the textview display the _xmlContent. I've set the binding in xcode to "delegate" and the key path to xmlContent (also tried self.xmlContent and _xmlContent and all variations thereof that I could think of).

    @import <Foundation/Foundation.j>
    @import <AppKit/AppKit.j>

    @implementation AppController : CPObject
    {
        @outlet CPWindow    theWindow;
        @outlet CPTextView  codeView;

        CPString _xmlContent @accessors(property=xmlContent);
    }

    // ...

    @end

The message I'm getting now is:

CPUndefinedKeyException: <AppController 0x00a177> is not key value coding-compliant for the key xmlContent

How do I make my AppController key value coding compliant for the key "xmlContent"?


Solution

  • Your code looks correct. I recreated it myself and it works fine for me:

    @implementation AppController : CPObject
    {
        @outlet CPWindow    theWindow;
    
        CPString _xmlContent @accessors(property=xmlContent);
    }
    
    - (void)applicationDidFinishLaunching:(CPNotification)aNotification
    {
    }
    
    - (void)awakeFromCib
    {
        [theWindow setFullPlatformWindow:YES];
        [self setXmlContent:@"kitten"];
    

    Xcode settings showing delegate binding to xmlContent

    Safari showing binding working

    You should verify that you are running the code you think you are running. One common pitfall here is that some web-browsers really like to hang on to cached copies of your code so make sure you have the "Disable Caches" option enabled in whatever browser you are testing with.

    If all else fails, change the port number of your development HTTP server and load the page from the new URL. This will force the browser to get fresh copies of everything (because the URL changed).