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
7
votes
1 answer

Swift Codable protocol… encoding / decoding NSCoding classes

I have the following struct… struct Photo: Codable { let hasShadow: Bool let image: UIImage? enum CodingKeys: String, CodingKey { case `self`, hasShadow, image } init(hasShadow: Bool, image: UIImage?) { …
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
7
votes
0 answers

State restoration crash reports (decodeObject?)

I'm getting these crash reports from XCode (I pasted one at the bottom) and I can't reproduce the error or figure out what the problem is. From the backtrace I'm thinking it could be related to the decodeObject stuff… I did have some problems with…
dbmrq
  • 1,451
  • 13
  • 32
7
votes
1 answer

Crash on decode of object after renaming and rewriting to Swift

Since we have renamed (Bestemming -> Place) the class and rewrote it from Objective-c to Swift some users experience crashes with it. We are trying to load an object from the NSUserDefaults with the NSCoding principle. The crash: Thread : Crashed:…
Sjoerd Perfors
  • 2,317
  • 22
  • 37
7
votes
3 answers

How do I get an optional from decodeIntegerForKey instead of 0?

My app saves settings to a file on an iOS device by archiving instances of a class with properties. The class uses the NSCoding protocol, and therefore, I encode these properties using encodeWithCoder. I then try to read these files back into…
KenL
  • 157
  • 9
7
votes
2 answers

How to deal with field type changes when using NSCoding

I have the following class that implements NSCoding and I have created several instances of it and persisted them to file. @interface BiscuitTin () @property NSString *biscuitType; @property int numBiscuits; @end @implementation BiscuitTin -…
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
7
votes
1 answer

NSKeyedArchiver unarchiveObjectWithFile crashes with EXC_BAD_INSTRUCTION

I have the following code, used to get the path of an object that has been archived let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) let path = paths[0] as…
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
7
votes
4 answers

NSUserDefaults NSObject with NSArray Object

I am trying to save object in NSUserDefaults, went throught many questions on this site but could not resolve the issue, my NSObject has an NSMutableArray of another object. like here the main object is HotelDC and it has an array "features" an…
Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
6
votes
3 answers

NSCode: encoder and decoder for primitive types

I was trying to create a generic encoder and decoder for my model classes. I was trying to find a way to call the "encode method" for all types of properties, either objects (NSString, NSNumber, NSArray, etc...) and primitive types. And I saw…
lao
  • 1,670
  • 20
  • 23
6
votes
2 answers

Using NSCoding on a subclass of custom class

I am using NSCoding to archive/unarchive a custom class as a method of data persistence. It works fine if the object is a subclass of NSObject, but I also have objects that are subclasses of custom objects. Do I need to change the initWithCoder:…
Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
6
votes
2 answers

"Value for key 'root' was unexpected class 'NSArray'" error when retrieve an array of object in UserDefault, Swift

I'm trying to retrieve an array of custom object in Swift, where i get the error "UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'NSArray'. Allowed classes are '{(MusicCloud.Playlist)}" My project is a music player, so…
hrtlkr29
  • 383
  • 1
  • 7
  • 21
6
votes
3 answers

Swift 2.0 Unit Testing for NSCoding

I am going to try and use the new testing features in Xcode 7 (code coverage) and Swift 2.0. Using code coverage, I see that I am not testing my NSCoding methods. For a trivial example of saving a few details, such as: required init(coder aDecoder:…
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
6
votes
1 answer

NSKeyedUnarchiver fails to decode a custom object in swift

I'm trying a basic implementation of the NSCoding protocol in swift, but it seems I can't success to unarchive an object after it has been correctly archived. Here's my attempt import Cocoa class User: NSObject, NSCoding { var name: String …
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
6
votes
1 answer

How to find all keys used in NSKeyedArchiver/NSKeyedUnarchiver

I have an NSData that was created by using NSKeyedArchiver. Is there a way to iterate over all the values inside it? It must somehow be possible to get all the keys that were stored in it when using +[NSKeyedUnarchiver…
stigi
  • 6,661
  • 3
  • 40
  • 50
6
votes
1 answer

initWithCoder: not visible in NSObject?

I have an interface: #import @interface Picture : NSObject; @property (readonly) NSString *filepath; - (UIImage *)image; @end and implementation: #import "Picture.h" #define kFilepath @"filepath" @interface Picture ()…
0xSina
  • 20,973
  • 34
  • 136
  • 253
5
votes
3 answers

Write custom object to .plist in Cocoa

I am blocking into something and I am sure it is too big. I have a custom object that look like this @interface DownloadObject : NSObject { NSNumber *key; NSString *name; NSNumber *progress; NSNumber *progressBytes; …
Dimillian
  • 3,616
  • 4
  • 33
  • 53