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

Compatibility of nskeyedarchiver between OS X and iOS

If I share a class implementing the NSCoding protocol between a desktop cocoa application and an iOS application, will I also be able to share a datafile created by archiving an object between those two applications?
Hosiers
  • 33
  • 2
3
votes
1 answer

NSKeyedUnarchiver data in wrong format

I'm using ARKit and GameKitMatches so I can't use Codable (afaik) because MCPeerID as well as ARWorldMap aren't codable, to get that out of the way first. So I'm using NSCoding and NSSecureCoding but for some reason I always catch the error: The…
thisIsTheFoxe
  • 1,584
  • 1
  • 9
  • 30
3
votes
2 answers

"The data couldn’t be written because it isn’t in the correct format" occurs while archiving a custom class with NSKeyedArchiver

I was trying a new API in iOS12: [NSKeyedArchiver archivedDataWithRootObject:<#(nonnull id)#> requiringSecureCoding:<#(BOOL)#> error:<#(NSError * _Nullable __autoreleasing * _Nullable)#>] What I was trying to do is very simple, archive a custom…
Davis Hoo
  • 31
  • 1
  • 3
3
votes
1 answer

Serializing/Storing a UIView and its subviews

I'm a relatively new iOS developer, with most of my previous experience coming from .NET. My application is a canvas like system in that there is a parent UIView that contains all the objects the user is placing/resizing/etc as subviews. I want to…
3
votes
2 answers

objects conforming to nscoding will not writetofile

I want to write an array of Question objects to file, but somehow writeToFile isn't doing anything. A Question has an Owner and and an array of Answer objects. An Answer has an Owner as well. All three of them conform to the NSCoding protocol (as…
SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39
3
votes
2 answers

Can't save Bool in Swift

I'm new to Swift (background in C++) and I'm trying to do a very simple thing: save a Bool. It works perfectly if I convert the bool to a string that is either "A" or "B" and then convert back but if I save the bool directly with encode and…
3
votes
1 answer

Why does an SKSpriteNode subclass work without encode(with:)

The NSCoding protocol states: Any object class that should be codeable must adopt the NSCoding protocol and implement its methods. The two required methods are init?(coder: NSCoder) and func encode(with: NSCoder). SKSpriteNode inherits from SKNode…
peacetype
  • 1,928
  • 3
  • 29
  • 49
3
votes
3 answers

How to save a generic custom object to UserDefaults?

This is my generic class: open class SMState: NSObject, NSCoding { open var value: T open var didEnter: ( (_ state: SMState) -> Void)? open var didExit: ( (_ state: SMState) -> Void)? public init(_ value: T) { …
Adrian
  • 19,440
  • 34
  • 112
  • 219
3
votes
1 answer

Catching exceptions when unarchiving using NSKeyedUnarchiver

We've got a Swift class which inherits from NSObject and implements NSCoding. We need to change the name of the class in code and in the archives on disk. Fortunately, we don't need to retain the data. Wiping the cached files and returning default…
Dale Myers
  • 2,703
  • 3
  • 26
  • 48
3
votes
1 answer

How do I encode [Character : Int] property using NSCoder in Swift 3?

import UIKit class Foo: NSObject, NSCoding { var cx: [Character : Int] init(cx: [Character : Int]) { self.cx = cx } // MARK: - required convenience init(coder aDecoder: NSCoder) { let cx =…
Max
  • 636
  • 3
  • 13
  • 28
3
votes
1 answer

Can setValuesForKeysWithDictionary be used with a nested dictionary

So I have an NSDictionary where one of the keys is an array of dictionaries. The class I'm mapping to has matching key names and setters. Can setValuesForKeysWithDictionary fill the sub-dictionaries for me? When I tried it, it seemed like it filled…
pseudopeach
  • 1,475
  • 14
  • 29
3
votes
1 answer

NSCoder decodeDouble of optional value

The decodeDouble on NSCoder returns a non-optional value, but I would like to identify whether a value was nil before it was encoded. This is my scenario: var optionalDouble: Double? = nil func encode(with aCoder: NSCoder) { if let…
MarkHim
  • 5,686
  • 5
  • 32
  • 64
3
votes
2 answers

Converting to Swift 3 broke custom class encoding/decoding

So I've just converted a small app from Swift 2.2 to Swift 3. I've gotten rid of the usual errors and bits of mop up required after the auto converter but I've got a run time issue that I can't work out. I've got a custom class that I am saving to…
SimonBarker
  • 1,384
  • 2
  • 16
  • 31
3
votes
1 answer

IOS (Swift) How to explore persistent file(s) during debug session?

First post on StackOverflow, after extensively using it for a long time. I'm building a small app (just to lear swift), and I have troubles with making some data persistent. I use NSCoding to achieve that. The problem is that when saving, the…
Louis. B
  • 287
  • 1
  • 8
3
votes
1 answer

Handling NSCoding Errors in Swift

How should errors related to NSCoding be handled in Swift? When an object is initialized using init?(coder:) it may fail to be initialized if the data is invalid. I'd like to catch these errors and appropriately handle them. Why is init?(coder:) not…
Anthony Mattox
  • 7,048
  • 6
  • 43
  • 59