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
2
votes
3 answers

Encoding/decoding doubles with NSCoding

How can I use -[NSCoder encodeBytes:length:] and -[NSCoder decodeBytesWithReturnedLength:] with a double? I have an object conforming to NSCoding with two double properties, and am currently using NSNumber to encode/decode the values: -…
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
2
votes
2 answers

When subclassing NSCoder, would I need to subclass collection classes (i.e:NSArray,NSDictionary) aswell?

I'm exploring the idea of subclassing NSCoder to read/write a proprietary file format. I'm starting to believe this might also require me to subclass NSArray and NSDictionary to override encodeWithCoder: to ensure they are encoded properly: -…
user1040049
2
votes
2 answers

Objective-C – Using NSCoding and updating your app to the App Store

I'm using NSCoding to encode my objects and save them to disk as a "caching" feature not having to download data every time my app is started. Right now I'm saving this data in the Documents folder of the app which I have read is not deleted when…
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
2
votes
0 answers

How to encode NSNetService?

I have a class that has these two ivars: @interface UserData : NSObject { NSString *name; NSNetService *service; } I wanted to encode this into a NSData object eventually, so I implemented NSCoding: - (id)initWithCoder:(NSCoder…
Yep
  • 653
  • 5
  • 20
2
votes
1 answer

NSKeyedUnarchiver.unarchiveTopLevelObjectWithData return nil

I want to save an array of objects into UserDefaults and load it back. When trying to unarchive the data it always returns nil.. any idea? This is my object: class DbInsideLevel: NSObject, NSSecureCoding { static var supportsSecureCoding:…
ytpm
  • 4,962
  • 6
  • 56
  • 113
2
votes
2 answers

Cannot figure out why my app crashes when I use NSKeyedArchivers / NSKeyedUnarchivers

I am developing my first iphone 'Diary' app, which uses custom 'Entry' objects that hold an NSString title, NSString text and NSDate creationDate. When I try to archive an NSMutableArray of Entry objects, and later retrieve them the next time the…
2
votes
0 answers

Decoding an object when original class is unavailable and class is unknown

This is very similar to this question: How can I decode an object when original class is not available?, but what's the best way to do this if the data I am unarchiving could be any class conforming to a certain protocol? As a simple example,…
Cody
  • 650
  • 9
  • 16
2
votes
1 answer

NSKeyedUnarchiver decodeObjectForKey: cannot decode object of class for key (NS.objects)

I looked through whole SO but still no answer. My app reports this problem: Fatal Exception: NSInvalidUnarchiveOperationException *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (App_Title.Products) for key…
S. Gissel
  • 1,788
  • 2
  • 15
  • 32
2
votes
1 answer

NSKeyedArchiver: casting Data returning nil - Swift

Well, investigated several similar topics here, done everything as suggested, but my computed property "previousUserData" returns me nil, when trying to cast the decoded object to my type. What's wrong? @objc class PreviousUserData: NSObject,…
Andrew V. Jackson
  • 273
  • 1
  • 4
  • 14
2
votes
2 answers

Initialize subclass that requires `init(coder: NSCoder)`

I want to subclass CAShapeLayer so it will hold a reference to the corresponding object in the data model: class CPCoursePointLayer : CAShapeLayer { let itsDataRef :CPCoursePoint let itsEventData :CPEventData required init?(coder…
user29809
  • 85
  • 7
2
votes
1 answer

iOS Xcode How to persist data with custom objects containing custom objects?

Android developer and iOS newb here. I have an app I'm migrating to iOS. I'm learning Xcode and Swift at the same time so be gentle please! :) My question is I simply want to save/retrieve a class such as below (in Java): public class CardDeck…
2
votes
1 answer

NSCoding AND Codable Properties <=> JSON Format <=> (Read/Write) File

I need to read/write properties that are Codable (e.g., Date) and NSCoding (e.g., NSMutableAttributedString) from/to a JSON-formatted file. After looking into how to read from and write to files using Codable, how to do so in the JSON format, and…
Optimalist
  • 313
  • 2
  • 11
2
votes
0 answers

Serialize Custom subclass of CLCircularRegion with extra an property in the class

I am using this class to create an object but when I archive and unarchive from and to UserDefaults it only gives me the value of trialUrl but not it's superclass properties.Since I don't know if CLCircularRegion supports Codable protocol I'm stuck…
2
votes
1 answer

When decoding object as Int using NSCoding error thrown is: fatal error: unexpectedly found nil while unwrapping an Optional value

I have a a set of objects in Swift which I am using NSCoding to save to disk so that they can be read into memory the next time the app is run. The item being saved is an array of Volume objects. Each instance of Volume has a volumeNumber Int which…
laurie
  • 965
  • 1
  • 11
  • 21
2
votes
1 answer

The optionality of init?(coder:) vs init(coder:). What to do if nil?

NSCoding requires init(coder:), but there is also the optional version of this method init?(coder:). What exactly should one do if this returns nil? Is this even an issue? Say you are initializing a large hierarchy of objects with init(coder:), with…
MH175
  • 2,234
  • 1
  • 19
  • 35