How can I decode an object when original class is not available?

后端 未结 5 797
面向向阳花
面向向阳花 2021-02-05 00:52

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 : NSO         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 01:24

    newacct's answer helped me with the class name, but I also had an issue where I changed a variable name in my Swift class from my Objective-C class. Using @objc() fixed that as well:

    Old Class:

    @interface JALProgressData : NSObject 
    
    @property (nullable, nonatomic, strong) NSString *platformID;
    @property (nonatomic, assign) NSTimeInterval secondsPlayed;
    @property (nonatomic, assign) NSTimeInterval totalDuration;
    @property (nullable, nonatomic, strong) NSDate *lastUpdated;
    
    @end
    

    New Class:

    @objc(JALProgressData)
    class VideoProgress: NSObject, NSCoding {
        @objc(platformID) var videoId: String?
        var secondsPlayed: NSTimeInterval?
        var totalDuration: NSTimeInterval?
        var lastUpdated: NSDate?
    }
    

    I also realized I was using encodeObject decodeObjectForKey for value types, which was causing issues with my Swift class. Switching to encodeDouble and decodeDoubleForKey for the NSTimeInterval types fixed aDecoder returning nil for those values.

提交回复
热议问题