small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
04 Oct 2009, 23:30
Keith Johnson (1 post)

1) When compiling the CDPreferences project, a warning comes for these lines of MyDocument.m

  //START:GoodRead
  if ([[[self defaults] valueForKey:@"default1"] boolValue]) {  // MyDocument may not respond to defaults
    //Do something clever
  }
  //END:GoodRead
  //START:GoodWrite
  [[self defaults] setValue:@"New Value" forKey:@"newKey"]; // MyDocument may not respond to defaults
  //END:GoodWrite
}

I believe “defaults” should be “preferences” so that it actually exercises the code, not just setting values on the defaults dictionary.

2) Upon expanding this example by binding a parameter to a UI element, there was a crash EXC_BAD_ACCESS when the binding tried to call [self defaults]… in setValue:forUndefinedKey:

It turns out there needed to be a retain somewhere along the line in passing the defaults dictionary in to the DocumentPreferences class.

Changing the property attribute to ‘retain’ fixed it. In DocumentPreferences.h: @property (retain) NSDictionary *defaults;

3) I am not sure about the Mutable/fixed nature of the defaults dictionaries. In MyDocument a NSMutableDictionary is created for defaults, which is handed to the DocumentPreferences and set on an NSDictionary (with assign/retain). I don’t know enough to say whether this works, but it seems a copy from mutable to fixed dictionary, or an assign from mutable to mutable would be correct. That’s a beginner’s Cocoa question, not a Core Data question, but may be relevant to tidying up the example code.

Thanks for the book!

You must be logged in to comment