NSKeyedUnarchiver unarchiveObjectWithFile: returns nil in init: method

前端 未结 2 1774
野性不改
野性不改 2021-01-04 23:22

Update: This works if I call archiveRootObject: from applicationDidFinishLaunching:. If I call it from the init: method of a singleton class, it returns nil.

2条回答
  •  再見小時候
    2021-01-04 23:25

    1. You must always -retain an unarchived object: Game *g2 = [[NSKeyedUnarchiver unarchiveObjectWithFile:archivePath] retain];

    2. Does your g2 conform to NSCoding? Make sure it does, if it doesn't declare in the g2 header. In the implementation file define methods -(id)initWithCoder:(NSCoder *)coder and -(void)encodeWithCoder:(NSCoder *)coder

    3. If you're struggling to get this to work consider archiving and unarchiving a standard NSObject, like an NSString or some such. You probably don't need to archive a whole custom object, maybe just a remaining time number, game location or position or score. In other words, archive and unarchive the bare minimum you need.

提交回复
热议问题