Re: [Pyobjc-dev] NSUserDefaultsController
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-04-12 23:48:46
|
On Apr 12, 2004, at 7:21 PM, Paul Donovan wrote: > Hi, > > I'm trying to use the new NSUserDefaultsController (part of Cocoa > Bindings). I've got some example code in Obj-C (from > http://homepage.mac.com/mmalc/CocoaExamples/controllers.html), but I'm > having trouble converting it to PyObjC. > > The original Obj-C code is: > > // Get font name and size from user defaults > NSDictionary *values = [[NSUserDefaultsController > sharedUserDefaultsController] values]; > NSString *fontName = [values valueForKey:@"FontName"]; > > So I've converted it to this: > > prefs = > NSUserDefaultsController.sharedUserDefaultsController().values() > print prefs > print prefs['FontName'] > > Results in: > > <_NSControllerObjectProxy: 0x1123830> > 2004-04-13 00:12:22.453 Test[19056] An exception has occured: > Traceback (most recent call last): > File > "/Users/pauld/Projects/Test/build/Test.app/Contents/Resources/ > __main__.py", line 18, in ? > AppHelper.runEventLoop(argv=[]) > File > "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ > python2.3/site-packages/PyObjC/PyObjCTools/AppHelper.py", line 71, in > runEventLoop > NSApplicationMain(argv) > File > "/Users/pauld/Projects/Test/build/Test.app/Contents/Resources/ > RatingsModel.py", line 46, in awakeFromNib > self.makeLibrary(file) > File > "/Users/pauld/Projects/Test/build/Test.app/Contents/Resources/ > RatingsModel.py", line 29, in makeLibrary > print prefs['FontName'] > TypeError: unsubscriptable object > > I'm not sure why the type of 'prefs' is an NSControllerObjectProxy. I > was expecting it to be an NSDictionary* (and hence a python > dictionary). I have an instance of an NSUserDefaultsController in my > nib file, so I'm a bit stuck at this point. > > I'm using pyobjc from anonymous CVS as of a couple of days ago, on > 10.3.3. It's not a NSDictionary or a subclass of NSDictionary so PyObjC doesn't know that it needs the __getitem__ mapping. It can't automatically say that all bridged objects should use valueForKey: for __getitem__ because some should map to objectAtIndex: You'll have to use the literal translation, at least for now: prefs.valueForKey_(u'FontName') Note that unicode strings are the literal translation of ObjC @"strings". Regular Python strings can potentially hit an exception at the bridge and always incur additional encoding overhead... and besides, you should probably get in the habit of using unicode for all text anyway. -bob |