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

encodeWithCoder: unrecognized selector sent to instance

I'm attempting use NSCoding protocol to read and write data to plist. I get an exception when I try to write the [GolfHoles] which is a subclass of NSObject. I've read several posts with different approaches, but none have helped. class GolfCourse:…
user2966301
  • 101
  • 1
  • 1
  • 4
3
votes
1 answer

PINCache objectForKey returning NSCoding and can't casting it

I'm using PINCache (https://github.com/pinterest/PINCache) to cache some objects into my app. So, using objC it is perfect, but when I wanna cast with swift I had EXC_BAD_ACCESS When I call objectForKey - (id )objectForKey:(NSString…
Klevison
  • 3,342
  • 2
  • 19
  • 32
3
votes
1 answer

Using NSCoding with Realm Model Objects

I have been banging my head on my monitor for weeks on this, and after taking a break by skirting around my issue, I find I'm running in to the very same issue all over again. First off, the closest thing I could find relating to my issue: NSCoding…
Tiki McFee
  • 228
  • 3
  • 11
3
votes
1 answer

NSCoding swift dictionary with swift tuple as values

I have a Swift dictionary with keys as Strings and values as Swift tuples. I would like to send this dictionary over to the other device so I need to implement NSCoding on this dictionary. Can anybody help me as to how can I achieve that. Following…
Vik Singh
  • 1,563
  • 2
  • 19
  • 36
3
votes
1 answer

SWIFT How to create a NSCoding Subclass and call it from another class?

I found this black of code on NSCoding and it almost does want I want it to. the link for where I found it is below. How do I create a NSCoding class and user in in other classes? The below code dies not work. I hope some can help me with this.…
LearningGuy
  • 191
  • 2
  • 12
3
votes
1 answer

What's the fastest way to determine if a file adheres to a particular class's NSCoding implementation?

Given: An application that accesses a directory of files: some plain text, some binary files that adhere to a particular NSCoding implementation, and perhaps other binary files it simply doesn't understand how to process. I want to: Be able to…
Justin Searls
  • 4,789
  • 4
  • 45
  • 56
3
votes
1 answer

How to persist and load an object which conforms to NSCoding protocol?

I have made an class which conforms to the NSCoding protocol and does all the encode and decode stuff. For my app, I simply want to persist an object from that class to the device and the next time when the app launches, I want to load that object…
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
3
votes
5 answers

How do I use NSSecureCoding to guarantee the content of collection classes?

I have an object of class SGBContainer which has an array named objects that contains objects of class SGBObject. Currently, they each implement NSCoding but not NSSecureCoding. The -initWithCoder: for SGBContainer looks like this: -…
Simon
  • 25,468
  • 44
  • 152
  • 266
3
votes
0 answers

Saving and retrieving custom object from NSUserdefaults is not working

I am trying to save a custom object to NSUserDefaults but it doesn't work. Here is the code. /// User.m //////////// #import "User.h" @implementation User .... + (User *)getUser { NSUserDefaults *defaults = [NSUserDefaults…
jdog
  • 10,351
  • 29
  • 90
  • 165
3
votes
1 answer

iOS archiving object with NSCoding failed

So I want to archive my object into by app sandbox documents directory. I copied most of the code from Big Nerd Ranch's iOS programming, so I don't see what could possibly go wrong. Is there anyway I could diagnose which part of the process is not…
harinsa
  • 3,176
  • 5
  • 33
  • 53
3
votes
3 answers

CGPathRef encoding

Is it possible to encode CGPathRef variables? I mean is there an encodeObject:forKey like method for CGPathRef variables?
cocoatoucher
  • 1,483
  • 3
  • 17
  • 22
3
votes
3 answers

Calling an optional method in superclass (Objective-C)

I'm trying to create a generic implementation for the NSCoding protocol. The code will be wrapped around in a macro the will implement NSCoding. In order to implement the protocol, we need two…
Tomer Shiri
  • 949
  • 1
  • 10
  • 19
3
votes
4 answers

How to load an Objective C Singleton using NSCoding?

I am writing a basic game, I am using a GameStateManager which is a singleton and handles all state management, such as saves, loads, delete methods. I have a separate singelton which handles all the Game stuff. The game object is inside the game…
zardon
  • 2,910
  • 6
  • 37
  • 58
2
votes
2 answers

NSCoding v/s Sqlite

I'm working on an iOS App fetching html content from a sqlite database and loading it into webviews through custom objective-c objects. I'm willing to implement NSCoding on these custom objects so as to avoid fetching content from sqlite. Anyone has…
Sooriah Joel
  • 111
  • 2
  • 6
2
votes
1 answer

NSCoding NSKeyedUnarchiver unarchiveObjectWithFile: returning null

I am trying to save some values from my app using NSCoding. I'm able to save the value but not able to retrieve it. Here's where I am declaring the protocol: @interface AddReminderEventViewController : UIViewController
Jazzmine
  • 1,837
  • 8
  • 36
  • 54