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

NSDictionary to custom class

I've a custom class QBChatDialog object, that I'm storing in sqlite database like -(void)storeInDB:(QBChatDialog *)dialog { NSString *query = = [NSString stringWithFormat:@"INSERT INTO dialogs (dialog_id,last_message) VALUES…
Sikander
  • 447
  • 4
  • 26
0
votes
2 answers

Swift: Trying to implement NSCoding

Im trying to add NSCoding protocol to my custom class: class Convo: NSObject, NSCoding { var name: String var pic: UIImage override init() { self.name = "Dror" self.pic = UIImage (named: "NoPic.png")! …
Dror Chen
  • 51
  • 1
  • 10
0
votes
1 answer

NSCoding and GameplayKit Classes

In my game I have a NSCoding-conforming Combat class, which also uses GameplayKit's GKGridGraph and GKGridGraphNode objects, which do not conform to NSCoding. Is there a way to serialize/unserialize these objects?
Rhuantavan
  • 445
  • 3
  • 17
0
votes
1 answer

archive UIImage with JsonModel

the situation is here: I use JsonModel to convert json to model when I get data from API, it's pretty good. and I have to do persistent storage for some data, I finally choose NSKeyedArchive and NSKeyedUnarchive to save and fetch data. now comes the…
childrenOurFuture
  • 1,889
  • 2
  • 14
  • 23
0
votes
0 answers

Is it necessary to implement NSCoding protocol when use NSKeyedArchiver?

The doc says archivedDataWithRootObject Returns an NSData object containing the encoded form of the object graph whose root object is given. it says encoded, but the parameter it receives is id _Nonnull, I think id < NSCoding > is more…
cajsaiko
  • 1,017
  • 1
  • 8
  • 15
0
votes
1 answer

iCloud - NSCoding or Core Data

I have an app working with NSCoding, 3 classes, unrelated - hence went with the simple NSCoding approach to save my array of objects. However, now the question of iCloud. From what I understand Core Data now works with iCloud basically out of the…
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
0
votes
1 answer

NSCoding objc_class

I have an NSDictionary with NSString keys and Class (objc_class) values , and i want to archive this dictionary using NSCoding protocol . how do i archive "Class" objects ?. I know how to implement the NSCoding protocol , what i want is how to…
m.eldehairy
  • 695
  • 8
  • 10
0
votes
1 answer

Read / Write Data NSCoding iOS - Singelton or Not?

This has been asked many times, but I'm still at a crossroads. My application is required to save data, this data can be accessed through many areas of the application. I wanted to avoid core data as it is overkill for what I need to save 3-4…
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
0
votes
2 answers

Approach to encoding/decoding nontrivial property of a class in Swift

Original Post I have a class that I'm using NSCoding to encode and decode. I get how to encode basic types like a number or a String, and I'm even chaining NSCoding compliant classes. I also get how to encode an array of these basic types. What I'm…
Tim Fuqua
  • 1,635
  • 1
  • 16
  • 25
0
votes
1 answer

Why does initWithCoder not return instancetype?

It seems that most init methods in Objective-C now tend to return instancetype instead of id. See [UIView initWithFrame:], [UIViewController initWithNibName:bundle:], [NSArray init] and siblings, etc. But initWithCoder uses id. Why is this? Has it…
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
0
votes
0 answers

SWIFT how to store a CLASS containing a STRUCT with an ARRAY of STRUCT inside USERDEFAULTS

the problem may sound weird but is exactly my situation. I have a class containing a struct with only an array inside: an array of another struct. I would like to store an object of the class into user default. Let see some code struct Inside { …
Nisba
  • 3,210
  • 2
  • 27
  • 46
0
votes
1 answer

Encode with coder not being called on lower level object

I'm updating an existing app to follow the MVC design. I created a top level data Model class. In this dataModel class I archive and dearchive a children array (self.children). This is an array of Child objects with an items property. The items…
leenyburger
  • 1,198
  • 1
  • 11
  • 23
0
votes
1 answer

IOS Save Association Array With NSKeyedArchiver

I'd like to save an NSObject using NSKeyedArchiver and save its association NSArray. A User has many skills (not more than 6 or 7). Skills are an NSArray of NSObjects with NSCoding protocol, in the User Model //User.h @property (strong, nonatomic)…
stefano_cdn
  • 1,362
  • 2
  • 15
  • 29
0
votes
1 answer

Swift - Core Data & NSCoding : Failed to call designated initializer

I am struggling with the following case in Swift : I have a NSManaged class called Event : import UIKit import Foundation import CoreData class Event: NSManagedObject, NSCoding { @NSManaged var eventArchived: Bool @NSManaged var…
H4Hugo
  • 2,570
  • 3
  • 16
  • 32
0
votes
1 answer

Better to archive 1 large object or several smaller objects with NSCoder?

I have a need to archive an array of objects of several different types. Each object has its own instance vars. Is it better to create singleton object with an array that holds ~20 archived objects, or have a single archived object that has an…
Calvin
  • 8,697
  • 7
  • 43
  • 51