Questions tagged [nscoding]

NSCoding is a protocol from Apple Foundation framework. The NSCoding protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded.

The NSCoding protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded. This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces).

These two methods in the NSCoding protocol are:

  • encodeWithCoder: Encodes the receiver using a given archiver. (required)
    -(void)encodeWithCoder:(NSCoder *)encoder

  • initWithCoder: Returns an object initialized from data in a given unarchiver. (required)
    - (id)initWithCoder:(NSCoder *)decoder

And in Swift :

  • init(coder decoder: NSCoder!)

  • func encodeWithCoder(_ encoder: NSCoder!)

604 questions
177
votes
10 answers

Xcode 9 GM - WKWebView NSCoding support was broken in previous versions

Does anyone know how to fix this error with Xcode 9 GM? I'm working on an app made with Xcode 8.3, the deployment target is for iOS 9.3 and I never had this problem before. I don't find any information here or on Apple forums yet :( Edit: This…
Mehdi Chennoufi
  • 2,193
  • 2
  • 11
  • 16
145
votes
6 answers

Why NSUserDefaults failed to save NSMutableDictionary in iOS?

I'd like to save an NSMutableDictionary object in NSUserDefaults. The key type in NSMutableDictionary is NSString, the value type is NSArray, which contains a list of object which implements NSCoding. Per document, NSString and NSArray both are…
BlueDolphin
  • 9,765
  • 20
  • 59
  • 74
94
votes
6 answers

Attempt to insert non-property list object when trying to save a custom object in Swift 3

I have a simple object which conforms to the NSCoding protocol. import Foundation class JobCategory: NSObject, NSCoding { var id: Int var name: String var URLString: String init(id: Int, name: String, URLString: String) { …
Isuru
  • 30,617
  • 60
  • 187
  • 303
87
votes
12 answers

Saving custom Swift class with NSCoding to UserDefaults

I am currently trying to save a custom Swift class to NSUserDefaults. Here is the code from my Playground: import Foundation class Blog : NSObject, NSCoding { var blogName: String? override init() {} required init(coder aDecoder:…
The Lone Coder
  • 3,062
  • 4
  • 20
  • 35
76
votes
1 answer

Got Unrecognized selector -replacementObjectForKeyedArchiver: crash when implementing NSCoding in Swift

I created a Swift class that conforms to NSCoding. (Xcode 6 GM, Swift 1.0) import Foundation private var nextNonce = 1000 class Command: NSCoding { let nonce: Int let string: String! init(string: String) { self.nonce =…
Hlung
  • 13,850
  • 6
  • 71
  • 90
64
votes
5 answers

Swift saving and retrieving custom object from UserDefaults

I have this in Playground using Swift 3, Xcode 8.0: import Foundation class Person: NSObject, NSCoding { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } required…
user773881
  • 845
  • 1
  • 8
  • 10
43
votes
5 answers

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

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 @property (copy, nonatomic) NSString *name @property (copy,…
Aнгел
  • 1,361
  • 3
  • 17
  • 32
31
votes
3 answers

When to use NSSecureCoding

I'm learning about the NSSecureCoding protocol introduced by Apple in iOS 6. From my understanding so far, it should be used whenever a class encodes/decodes instances of itself, in order to prevent substitution attacks. I'm wondering whether it…
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
30
votes
2 answers

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject

The FooBar class below has to override the == function of the Equatable type. However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is…
Crashalot
  • 33,605
  • 61
  • 269
  • 439
25
votes
4 answers

NSCoding VS Core data

I've been searching for an article that explains NSCoding (NSKeyedArchiver...) advantages and disadvantages over use of CoreData (SQLite....). There's a lot of options, I can implement my own custom binary reader/writer, or use plists/xml/json...…
LolaRun
  • 5,526
  • 6
  • 33
  • 45
22
votes
2 answers

When is "required init?(coder aDecoder: NSCoder)" called on a UIView or UIViewController?

When I create a subclass of UIView or UIViewController with a stored property, Xcode will not compile my project unless I include an implementation of required init?(coder aDecoder: NSCoder). Currently, I have the following implementation to shut…
kmell96
  • 1,365
  • 1
  • 15
  • 39
22
votes
3 answers

NSCoding of NSMutableDictionaries containing custom objects

I was trying to serialize a SearchEntity object(custom object) containing an NSMutableDictionary containing a set of type CategoryEntity(custom object). 1 SearchEntity containing: 1 NSMutableDictionary (parameters) parameters…
RickiG
  • 11,380
  • 13
  • 81
  • 120
19
votes
4 answers

archive array of optional structs with NSCoding in Swift?

I've done a lot of NSCoding archiving in Obj-C, but I'm not sure how it handles structs in Swift, nor arrays with optional values. Here is my code: public struct SquareCoords { var x: Int, y: Int } and here is the class which I need to…
Chuck Smith
  • 2,101
  • 3
  • 16
  • 21
17
votes
3 answers

NSSecureCoding trouble with collections of custom class

I am having trouble with adopting NSSecureCoding. I encode an array containing objects of my custom class, which adopts NSSecureCoding properly. When I decode it, passing the class NSArray (which is the class of the object I encoded), it throws an…
user102008
  • 30,736
  • 10
  • 83
  • 104
17
votes
1 answer

Encoding an Objective-c Block?

Is it possible to encode an Objective-C block with an NSKeyedArchiver? I don't think a Block object is NSCoding-compliant, therefore [coder encodeObject:block forKey:@"block"] does not work? Any ideas?
rebo
  • 1,067
  • 1
  • 7
  • 10
1
2 3
40 41