nscoding
How can I decode an object when original class is not available?
This might be another solution, and it’s what I did (since I didn’t use Swift at that time). In my case, I archived an object of class “City” but then renamed the class to “CityLegacy” because I created a completely new “City” class. I had to do this to unarchive the old “City” object as … Read more
Got Unrecognized selector -replacementObjectForKeyedArchiver: crash when implementing NSCoding in Swift
Although Swift class works without inheritance, but in order to use NSCoding you must inherit from NSObject. class Command: NSObject, NSCoding { … } Too bad the compiler error is not very informative 🙁
Saving custom Swift class with NSCoding to UserDefaults
In Swift 4 or higher, Use Codable. In your case, use following code. class Blog: Codable { var blogName: String? } Now create its object. For example: var blog = Blog() blog.blogName = “My Blog” Now encode it like this: if let encoded = try? JSONEncoder().encode(blog) { UserDefaults.standard.set(encoded, forKey: “blog”) } and decode it like … Read more
Attempt to insert non-property list object when trying to save a custom object in Swift 3
You need to create Data instance from your JobCategory model using JSONEncoder and store that Data instance in UserDefaults and later decode using JSONDecoder. struct JobCategory: Codable { let id: Int let name: String } // To store in UserDefaults if let encoded = try? JSONEncoder().encode(category) { UserDefaults.standard.set(encoded, forKey: UserDefaultsKeys.jobCategory.rawValue) } // Retrieve from UserDefaults … Read more
Why NSUserDefaults failed to save NSMutableDictionary in iOS?
I found out one alternative, before save, I encode the root object (NSArray object) using NSKeyedArchiver, which ends with NSData. Then use UserDefaults save the NSData. When I need the data, I read out the NSData, and use NSKeyedUnarchiver to convert NSData back to the object. It is a little cumbersome, because i need to … Read more
Xcode 9 GM – WKWebView NSCoding support was broken in previous versions
The error is correct behavior, and not a bug in Xcode 9. Although WKWebView was introduced in iOS 8, there was a bug in -[WKWebView initWithCoder:] that was only fixed in iOS 11, which always crashed at runtime and thus prevented configuring one within Interface Builder. https://bugs.webkit.org/show_bug.cgi?id=137160 Rather than allow developers to build something in … Read more