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

Advantages and disadvantages of encoding objects with NSCoding or simply writing data to files

I'm curious what the advantages of encoding objects in objective c with NSCoding and writing them to disk may be over simply writing a persistence object to disk. Is there a performance increase in terms of I/O or disk space usage?
Casey Flynn
  • 13,654
  • 23
  • 103
  • 194
2
votes
1 answer

NSCoding in Swift 3 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.User encodeWithCoder:]

I have issues to implement NSCoding. Here is my code for User Class: public class User: NSCoder { ... NSCoding methods: Decoder: required public init(coder aDecoder: NSCoder) { self.deviceToken = aDecoder.decodeObject(forKey:…
Josep Escobar
  • 445
  • 4
  • 11
2
votes
0 answers

Swift 3 how to encode struct & enum with NSCoding

I have a animation class as following: enum AnimationState: String { case idle = "idle" case walk = "walk" } struct Animation { let animationState: AnimationState //let facingDireaction: FacingDirection let speed: TimeInterval …
Bob
  • 155
  • 1
  • 8
2
votes
0 answers

Difference approach when persisting data

I need to fetch the data of merchant and display it into TableView, i also want to persist the data, but I'm still confused on what approach should I use. Just also want to ask the difference between this two approach, especially their…
vidalbenjoe
  • 921
  • 13
  • 37
2
votes
1 answer

Swift NSCoding - How to read an encoded array

I'm using Swift playground to write a parent-child relationship using NSCoding. The relationship can be described as: One Author can write many Books However, it causes an error when I try to save the collection of books; Playground execution…
zardon
  • 1,601
  • 4
  • 23
  • 46
2
votes
1 answer

NSCoding : Found nil unwrapping an Optional value

since updating to Swift 3 I get this very known crash that I'm not able to solve by myself... : fatal error: unexpectedly found nil while unwrapping an Optional value* : on line self.isDefault = aDecoder.decodeObject(forKey:…
Lucas Adam
  • 59
  • 4
2
votes
2 answers

Swift 3 - NSCoding without the annoyance of inheriting from NSObject

Getting a crash from NSKeyedArchiver 2016-10-06 17:06:06.713 MessagesExtension[24473:2175316] *** NSForwarding: warning: object 0x61800009d740 of class '' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector…
S.Moore
  • 1,277
  • 17
  • 17
2
votes
0 answers

NSManagedObject with NSCoding not working

I have a Swift 3 iOS app in which I want to be able to share an entity I store in Core Data. What I've done is implement NSCoding in the NSManagedObject class: import Foundation import CoreData import UIKit public class Event:…
Deddiekoel
  • 1,939
  • 3
  • 17
  • 25
2
votes
1 answer

Best practice of copying and serializing Quartz references

I have objects containing Quartz-2D references (describing colors, fill patterns, gradients and shadows) in Cocoa. I would like to implement the NSCoding protocol in my objects and thus need to serialize those opaque Quartz-2D structures. Possible…
user8472
  • 3,268
  • 3
  • 35
  • 62
2
votes
1 answer

Can I Override swift per-module namespaces for my NSCoding classes? (I need Module1.MyCodedClass == Module2.MyCodedClass)

Can I Override swift per-module namespaces for my NSCoding classes? I basically need: Module1.MyCodedClass == Module2.MyCodedClass I know I can put MyCodedClass in a dynamic framework and I use that approach but that seems like overkill :D maybe…
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
2
votes
3 answers

Export Class with String & NSImage Properties to File

I have a class that I'd like to be able to open, and export to a file. It doesn't need to be edited - this is simply a way to share part objects CoreData database with other users of my app, so I don't think I need the complexity of subclassing…
glenstorey
  • 5,134
  • 5
  • 39
  • 71
2
votes
0 answers

How to reference existing objects during NSCoding state-restoration

I have a class that implements NSCoding and holds a reference to a UIView object... class A: NSObject, NSCoding { var view: UIView! init(view: UIView) { super.init() commonInit(view) } required init?(coder aDecoder:…
sleep
  • 4,855
  • 5
  • 34
  • 51
2
votes
2 answers

Swift, Trouble Instantiating NSCoding Compliant Class

Ive been hitting a roadblock with NSCoding. Specifically, instantiating a class that conforms to NSCoding. Maybe I'm missing something really obvious, but I haven't found any answers yet. ```swift class TitleTextField: UITextField,…
R.P. Carson
  • 439
  • 5
  • 20
2
votes
1 answer

How to encode a player entity using NSCoding - Swift

I want to save my game progress by saving the player's current status, such as his position on map, HP, mana, current state, etc.. I need help on how to convert the player object to NSdata and store it in NSUserDefaults. I have the following…
Bob
  • 155
  • 1
  • 8
2
votes
1 answer

Save Array of dictionaries with Enum type, NSCoding

I have been trying to figure out how to save an arrays of dictionaries using Enum types with NSCoding. I have an enum with Medal types enum Medal: String { case Unearned = "NoMedal" case Gold = "GoldMedal" case Silver = "SilverMedal" } I…
crashoverride777
  • 10,581
  • 2
  • 32
  • 56