I'm trying to learn PyObjC by translating the examples in Aaron 
Hillegass's "Cocoa Programming for Mac OS X" to Python.  I've run 
into a problem with the archiving feature described in chapter 8.  I 
have an NSMutableArray of Person objects, where Person is a subclass 
of NSObject.  I can turn the array into an NSData object with 
NSKeyedArchiver.archvedDataWithRootObject_(), but when I call 
NSKeyedUnarchiver.unarchiveObjectWithData_() I get back an empty 
array.

Any suggestions?

Here's simplified code that demonstrates the problem:

from Foundation import *

class Person(NSObject):
        def init(self):
                self = super(Person, self).init()
                if self is None: return self
                self.setPersonName_(u"New Person")
                return self

        def initWithCoder_(self, inCoder):
                self = super(Person, self).init()
                if self is None: return self
                self.setPersonName_(inCoder.decodeObjectForKey_('personName'))
                print 'Person.initWithCoder_ personName =', self.personName()

        def encodeWithCoder_(self, inCoder):
                print 'Person.encodeWithCoder_ personName =', self.personName()
                inCoder.encodeObject_forKey_(self.personName(), 'personName')

        def description(self):
                return super(Person, self).description() + '(' + 
self.personName() + ')'

        def personName(self):
                return self._personName

        def setPersonName_(self, inPersonName):
                self._personName = inPersonName

ma = NSMutableArray.alloc().init()
p1 = Person.alloc().init()
p1.setPersonName_('John Doe')
ma.insertObject_atIndex_(p1, 0)
print 'ma =', ma.description()

madata = NSKeyedArchiver.archivedDataWithRootObject_(ma)

decodedma = NSKeyedUnarchiver.unarchiveObjectWithData_(madata)
print 'decodedma =', decodedma.description()

--

Expected output:

ma = (<Person: 0x62e810>(John Doe))
Person.encodeWithCoder_ personName = John Doe
Person.initWithCoder_ personName = John Doe
decodedma = (<Person: 0x62e810>(John Doe))

Seen:

ma = (<Person: 0x62e810>(John Doe))
Person.encodeWithCoder_ personName = John Doe
Person.initWithCoder_ personName = John Doe
decodedma = ()

Thanks for any suggestions,
-- 
Jim Matthews
Fetch Softworks
http://fetchsoftworks.com
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to