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

Archiving Custom Object in Swift - Extended

I am working on an iOS App on Swift 4.0. The app uses an 3rd party SDK where there is a model lets say, class Customer: NSCopying, NSObject { var name: String! var age: Int! var address: Address! } At that point I have no control to…
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
4
votes
5 answers

Is it possible to serialize custom objects to a plist file in Objective-C?

For the sake of simplicity, let's assume that we have the following simple class interface: @interface Person : NSObject { NSString *firstname; NSString *lastname; } @property (copy) NSString *firstname; @property (copy) NSString…
t6d
  • 2,595
  • 3
  • 26
  • 42
4
votes
1 answer

How to add reusable custom views programmatically? (Swift)

I've created a reusable xib file that contains a table and it's being loaded in a TableView.swift file like this: required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) Bundle.main.loadNibNamed("TableView", owner: self,…
John R Perry
  • 3,916
  • 2
  • 38
  • 62
4
votes
1 answer

NSCoding protocol question

I want to add the archiving (NSCoding) protocol to my model class, and then i implement both methods encodeWithCoder:(NSCoder*)coder and initWithCoder:(NSCoder*)coder. MyModelClass has 2 instance variables (NSString and NSImage), so i use the…
Hebbian
  • 344
  • 5
  • 16
4
votes
1 answer

Saving Structs in Swift

Having just watched a couple of videos on value types in Swift from WWDC this year, Building Better Apps With Value Types in Swift Protocol-Oriented Programming in Swift I'm finding myself fully ready to embrace value types in my apps. This means…
promacuser
  • 374
  • 1
  • 15
4
votes
4 answers

Converting CLLocationCoordinate2D to a String that can be stored

I'm trying to save the coordinates of a user while in one ViewController so that it can be used to create an Annotation that can displayed in another ViewController. In the view controller that stores the coordinates I'm using the code…
Trip Phillips
  • 430
  • 1
  • 5
  • 18
4
votes
2 answers

How to make a deep copy with copyWithZone to duplicate a structure?

I have a class that represents a structure. This class called Object has the following properties @property (nonatomic, strong) NSArray *children; @property (nonatomic, assign) NSInteger type; @property (nonatomic, strong) NSString *name; @property…
Duck
  • 34,902
  • 47
  • 248
  • 470
4
votes
1 answer

How to migrate an object which has been persisted with NSKeyedArchiver?

I'm using a version of Archiver and have run into an issue. In a previous version of my project a class, Challenge was serialized to disk //v1.0 @interface Challenge : NSObject { ... @property (nonatomic,strong) NSString *challengeId; …
Damo
  • 12,840
  • 3
  • 51
  • 62
4
votes
2 answers

Swift enum and NSCoding

I have a 'Thing' object with a String property and an NSImage property; the Thing class has encodeWithCoder: and decodeWithCoder: methods, and I can archive and unarchive a [Thing] array using NSKeyedArchiver/Unarchiver. So far, so good. Now I want…
green_knight
  • 1,319
  • 14
  • 26
4
votes
1 answer

Why isn't there a default implementation of NSCoding?

I understand how to use NSCoding to convert my objects to archive objects. That's not my question. What I'm wondering is why there isn't a default implementation of NSCoding that could handle probably 99% of cases. For instance, every time I write a…
mbm29414
  • 11,558
  • 6
  • 56
  • 87
4
votes
2 answers

Encoding c-struct with Mantle (NSCoding)

I want to use Mantle framework (https://github.com/github/Mantle) to support NSCoding for my class with struct property: typedef struct { int x; int y; } MPoint; typedef struct { MPoint min; MPoint max; } MRect; @interface MObject…
podkovyr
  • 135
  • 3
  • 11
4
votes
3 answers

Archiving / Unarchiving results in initForReadingWithData incomprehensible archive

I've implemented an save on applicationWillTerminate and load on applicationWillFinishLoading. There is a complete object tree, all implement the NSCoding protocol and I've check the types I enter. One of the classes also stores an NSMutableData to…
Ger Teunis
  • 945
  • 1
  • 14
  • 30
4
votes
1 answer

Objective-C - readonly NSMutableArray and NSCoding?

I have an NSMutableArray, that should be only modified by the owner object (MyObject). so the first attemp was to make it a readonly property. The problem is that this class implements NSCoding, and NSCoding requires the archived objects to be a…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
4
votes
1 answer

Encoding an NSMutableArray

I have an object containing various NSString objects and variables which I us NSCoding to archive to a file on the disk and later unarchive. So far everything has been working perfectly. Today, I wanted to add an NSMutableArray to the object and…
Greg Steiner
  • 647
  • 1
  • 9
  • 20
3
votes
3 answers

How do NSCoder and/or NSKeyedUnarchiver handle multiple decodings of the same object?

I was wondering how NSCoder would handle an object that was shared and encoded by multiple objects the next time it was decoded. Will it make two copies of the object, or will one object be decoded and shared between all other objects that decode…
Eric L
  • 640
  • 6
  • 20