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

Can NSCoding and Codable co-exist?

In testing how the new Codable interacts with NSCoding I have put together a playground test involving an NSCoding using Class that contains a Codable structure. To whit struct Unward: Codable { var id: Int var job: String } class Akward:…
ArtbyKGH
  • 163
  • 3
  • 7
5
votes
1 answer

Storing json data on the iPhone: save the json string as it is VS make an object from json and use NSCoding + NSKeyedArchiver

In my iPhone application I get json data from a remote server, parse it using the Json Framework and present it in a UIview. Also want to be able to give the user the option to store the data on the device so that it can also be viewed offline. I…
Kremk
  • 149
  • 2
  • 8
5
votes
2 answers

Cocoa Interface Builder object initialization

Base on the documentation and sample code that I have gone through, I got an impression that when a class defined in xcode is read into and configured in Interface Builder, an object based on the class is effectively created and stored in an xib or…
Stanley
  • 4,446
  • 7
  • 30
  • 48
5
votes
1 answer

Swift 3 Xcode 8 - SwiftValue encodeWithCoder - unrecognized selector sent to instance

My custom objects conform to the NSCoding protocol with the following methods required init(coder decoder: NSCoder) { super.init() createdDate = decoder.decodeObject(forKey: "created_date") as? Date userId =…
MechEngineer
  • 1,399
  • 3
  • 16
  • 27
5
votes
1 answer

How to store the custom data like CBPeripheral via nsuserdefaults in swift?

I am developing in Swift. I want to store the custom data via nsuserdefaults. My custom data is like the following In ConnectedDevice.swift import UIKit import Foundation import CoreBluetooth import CoreLocation class ConnectedDevice : NSObject ,…
Wun
  • 6,211
  • 11
  • 56
  • 101
5
votes
1 answer

Changing from NSUserDefaults to NSCoding

I'm trying to create my first sprite kit swift game, and I have my game data working using nsuserdefaults. I want to make it more safe so I'm attempting to transition to NSCoding, but nothing I've found online has helped me completely and has just…
AEkon
  • 119
  • 6
5
votes
1 answer

How to call the validateValue method

I'm trying to make a generic NSCoding implementation and have a problem with decoding when the type of the object has changed in the mean time because of a newer version of the app. I have a problem with calling the validateValue method from Swift.…
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
5
votes
1 answer

Sharing an array of custom objects with Today Extension (widget) with NSUserDefaults

this is my first stack post so please be constructive when reviewing my posting technique! Basically, my problem is that I have an array of custom objects that I need to share with a today extension. The objects represent tasks in a to-do list, and…
Swankzilla
  • 115
  • 1
  • 6
5
votes
1 answer

What is the relationship between NSCoding and NSData?

I'm new to iOS programming and I've just learned some basics about saving/loading objects. In my book there is an example of saving an image to file: NSData *data = UIImageJPEGRepresentation(someImage, 0.5); [data writeToFile:imagePath…
wz366
  • 2,840
  • 6
  • 25
  • 34
5
votes
8 answers

Serializing a multidimensional array containing references to NSManagedObjects in Core Data

I have two entities, Chain and Step. Chain has an attribute steps, which may be a multidimensional array of Step entities, for example: [ step, step, step, [ step, step ], step ] Each Step object has a content property that is a…
maxedison
  • 17,243
  • 14
  • 67
  • 114
5
votes
4 answers

NSCoding and saving/loading default values issue

I'm writing some code for an iPhone app, and I'm having issues getting default data to load in correctly. I am basing my code off some example from the "Learning Cocos2d" book by Ray Wenderlich. It seems that even when I delete the app outright…
Mark Tuttle
  • 185
  • 1
  • 1
  • 9
4
votes
1 answer

NSKeyedArchiver returning unexpected class?

I have a custom class that extends NSString. I'm attempting to serialize it (for drag/drop) using an NSKeyedArchiver. The class overrides the ...Coder methods: - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super…
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
4
votes
1 answer

When decoding an object from NSCoder, what's the best way to abort?

I'm decoding a custom object from a cached serialization. I've versioned++ the object since it was encoded, and if the serialized version is an old version I want to just throw it away. I was under the impression that I could just return nil from…
DougW
  • 28,776
  • 18
  • 79
  • 107
4
votes
3 answers

Pluggable custom-view Nibs (Nib-in-a-Nib): Memory leak – why?

Our current best-practice for custom views is: Build the custom view in a Nib. In the view controller, programmatically load the Nib, get the custom view from the array of loaded objects (we do this in a UIView category method…
Yang Meyer
  • 5,409
  • 5
  • 39
  • 51
4
votes
2 answers

Array map, Swift

I have a custom Rules class. class Rules: NSCoding { var x: String? var y: Double? override func mapping(map: Map) { self.x <- map["x"] self.y <- map["y"] } In my viewModel I need to create an object rules and to pass 1 by 1 the…
ConteOlaf
  • 75
  • 2
  • 8