我有一个iOS 7应用程序,该应用程序将自定义对象作为文件保存到应用程序的iCloud Docs文件夹中.为此,我使用了NSCoding协议.
I have an iOS 7 application that saves a custom object to app's iCloud Docs folder as a file. For this, I make use of NSCoding protocol.
@interface Person : NSObject <NSCoding> @property (copy, nonatomic) NSString *name @property (copy, nonatomic) NSString *lastName @end对象序列化在iOS 7版本的应用程序中完美运行:
Object serialization works perfectly in iOS 7 version of the app:
initWithCoder和encodeWithCoder
[NSKeyedArchiver archivedDataWithRootObject:person]
person = NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)theData]
但是我需要将此应用程序移至iOS 8,并且此类将以该新iOS 8版本的应用程序快速编码并重命名".
But I need to move this app to iOS 8, and this class will be coded in swift and 'renamed' for this new iOS 8 version of the app.
class PersonOldVersion: NSObject, NSCoding { var name = "" var lastName = "" }当我尝试取消归档对象时,出现以下错误:
When I try to unarchive the object I got the following error:
*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Person)'我已经尝试将快速类'PersonOldVersion'重命名为原始类名('Person'),但仍然失败.
I already tried renaming swift class 'PersonOldVersion' to original class name ('Person') but still fails.
如何解码原始类不可用的对象?
How can I decode an object which original class isn't available?
推荐答案如果Objective-C中类的名称很重要,则需要显式指定名称.否则,Swift将提供一些错误的名称.
If the name of the class in Objective-C is important, you need to explicitly specify the name. Otherwise, Swift will provide some mangled name.
@objc(Person) class PersonOldVersion: NSObject, NSCoding { var name = "" var lastName = "" }更多推荐
当原始类不可用时,如何解码对象?
发布评论