Questions tagged [nscoder]

The NSCoder abstract class declares the interface used by concrete subclasses to transfer objects and other Objective-C/Swift data items between memory and some other format.

183 questions
67
votes
3 answers

How do I encode enum using NSCoder in swift?

Background I am trying to encode a String-style enum using the NSCoding protocol, but I am running into errors converting to and back from String. I get the following errors while decoding and encoding: String is not convertible to Stage Extra…
kfmfe04
  • 14,193
  • 11
  • 67
  • 132
48
votes
2 answers

Objective C - How do I use initWithCoder method?

I have the following method for my class which intends to load a nib file and instantiate the object: - (id)initWithCoder:(NSCoder*)aDecoder { if(self = [super initWithCoder:aDecoder]) { // Do something } return self; } How…
aryaxt
  • 69,636
  • 87
  • 281
  • 421
14
votes
1 answer

How to replace NSKeyedArchiver's initializer init(forWritingWith:) in iOS 12 to encode the metadata of a CKRecord

In iOS 12, the NSKeyedArchiver's initializer init(forWritingWith:) was deprecated. Xcode 10 recommends replacing it with the new initializer init(requiringSecureCoding:). The problem is that this initializer only sets the value of the…
Mikiko Jane
  • 276
  • 5
  • 11
13
votes
0 answers

Can I get all keys of a NSKeyedUnarchiver?

I want do something with these step: Encode a kind of object A with NSKeyedArchiver M and I can get a NSData X. Init another kind of object B with NSKeyedUnarchiver N from NSData X. The names of A and B are the same, so it's possible(I have done an…
user509027
  • 131
  • 5
12
votes
2 answers

-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance

Getting error when trying to utilize NSCoder Player.swift: class Player: NSObject, NSCoding { private var _playerName: String! private var _playerScore: Int! private var _playerColor: PlayerColor! //PlayerColor is an enum var…
Macness
  • 1,206
  • 2
  • 13
  • 23
9
votes
0 answers

Saving and restoring NSWindow state: can't restore firstRepsonder

I'm working on using encodeRestorableState/restoreStateWithCoder to save and restore the state of a window. (I'm doing this manually because I want to explicitly save my window state in the document and not just rely on the OS restoring its state as…
Uncommon
  • 2,903
  • 2
  • 13
  • 31
9
votes
2 answers

What does the question mark means in public init?(coder aDecoder: NSCoder)?

I don't think the question mark in public init?(coder aDecoder: NSCoder) means optionals. Also, when I override it I find I don't need to write the question mark at all. So what does it mean exactly ? --- Update -- The comment below had helped me…
Qiulang
  • 5,733
  • 4
  • 47
  • 82
8
votes
3 answers

Collection was mutated while being enumerated- archiving data and writing to file with NSCoder

In my app I am periodically writing a set of dynamic data to file. The data object gets updated about every second. Occasionally I get a "Collection was mutated while being mutated" exception on one of the lines in my encodeWithCoder: method. Each…
eric.mitchell
  • 8,501
  • 12
  • 50
  • 91
8
votes
4 answers

Crash on iOS device when dereferencing a pointer returned by NSCoder's decodeBytesForKey

I've found an unusual crasher with NSCoder when using the Apple LLVM Compiler 3.0 and compiled with -O3. It only crashes on devices. I've tested an iPhone 4 running iOS 5, an iPad 2 running iOS 5 and an iPad 1 running iOS 4. All crash identically.…
Matt Comi
  • 436
  • 4
  • 8
8
votes
2 answers

What does it mean to be NSCoding-Compliant?

So I'm going through this tutorial, and I've finally figured out how to archive an object using NSCoding, and also to initialize it from the filesystem again with the failable initializer. // To encode the object in the first place func encode(with…
user7024499
  • 881
  • 1
  • 9
  • 21
8
votes
1 answer

How to encode/decode a long long property with NSCoder?

How to decode a long long type property in initWithCoder: and encode in encodeWithCoder: with NSCoder in iOS? Thanks.
fannheyward
  • 15,632
  • 10
  • 61
  • 99
8
votes
1 answer

In initWithCoder: what are the keys in the NSCoder (a UINibDecoder)? (for UIImageView)

Specifically, I'm trying to find the image path. This would be a super useful thing to be able to get, and as far as I've found nobody knows how. I've looked at the generated nib file for keys, and I'm able to see the image URL in there (test.jpg)…
Eli
  • 4,584
  • 6
  • 38
  • 49
7
votes
0 answers

State restoration crash reports (decodeObject?)

I'm getting these crash reports from XCode (I pasted one at the bottom) and I can't reproduce the error or figure out what the problem is. From the backtrace I'm thinking it could be related to the decodeObject stuff… I did have some problems with…
dbmrq
  • 1,391
  • 11
  • 28
7
votes
1 answer

NSCoder and custom types

How do you use NSCoder to encode and decode custom types? For example, how would you use NSCoder with an instance of "STATE" where: typedef enum { ON, OFF } STATE;
SK9
  • 29,437
  • 32
  • 112
  • 154
7
votes
3 answers

What is the new pattern for releasing self with automatic reference counting?

Using the NSObject method -(id)awakeAfterUsingCoder:(NSCoder *)decoder as an example, the documentation says: Allows an object, after being decoded, to substitute another object for itself. For example, an object that represents a font might,…
1
2 3
12 13