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

Adding NSCoding as an Extension

I'd like to extend a framework class (I don't want to edit the source code directly), and make it conform to NSCoding. Basically, here's a simplification of the situation I'm in : /* Can't be edited. */ class Car: NSObject { var color:…
DCMaxxx
  • 2,534
  • 2
  • 25
  • 46
15
votes
2 answers

Saving Array with NSCoding

I have a small app that has a few saving functionalities. I have a data model class called: Closet: class Department: NSObject, NSCoding { var deptName = "" var managerName = "" var Task: [Assignment]? // <----- assignment class is in…
Amit
  • 310
  • 1
  • 3
  • 15
15
votes
1 answer

Is it necessary to call [super encodeWithCoder] when subclassing a object that implements NSCoding?

I know that when you write the initWithCoder method of a subclass of an object that implements NSCoding you have to call super initWithCoder (instead of super init), but do I have to call super encodeWithCoder in the implementation of…
XaitormanX
  • 891
  • 1
  • 11
  • 22
14
votes
2 answers

cast NSString! to String in swift

I have a instance variable name in String var name: String My class implements the NSCoding protocol. So for name I had func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(self.name, forKey: kName) } required init(coder aDecoder:…
Congruent Tech. UG
  • 1,408
  • 2
  • 12
  • 21
13
votes
4 answers

swift: nscoding decodeObject with nil all the time

I got the following codes on writing an object named Packet and send to the other side through Multipeer connectivity. However, I got the following error whenever it try to decode the encoded object. class Packet : NSObject, NSCoding { var…
user6539552
  • 1,331
  • 2
  • 19
  • 39
12
votes
1 answer

Enforcing types with NSSecureCoding

I decided to use NSSecureCoding over NSCoding, but I'm having trouble getting it to work. I would expect the following code to fail, since I'm encoding an NSString but attempting to decode an NSNumber. The object is initialized without throwing an…
Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
12
votes
3 answers

Swift: Does not conform to protocol NSCoding

I am trying to use the NSCoding protocol on a class I have written in swift, but cannot seem to figure out why the compiler complains that it "does not conform to protocol NSCoding" when I do implement the required methods: class ServerInfo:…
lomokat
  • 373
  • 1
  • 5
  • 13
12
votes
2 answers

iPhone - Why does the documentation say UIImageView is NSCoding compliant?

Ideally an NSCoding compliant class will work as expected using encodeWithCoder: and initWithCoder: (at least I thought so till recently) without the developer having to bother about what goes on inside the routines (unless my idea of an NSCoding…
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
11
votes
3 answers

How to convert NSValue to NSData and back?

A have a number of NSValue (obtained via KVC valueForKey) that I need to append to an NSData object in order to send it over the network using Game Center. Obviously I will also need to convert the NSValue back from NSData for more KVC…
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
11
votes
2 answers

NSCoding with Nested Custom Objects?

I have a series of nested objects that I am needing to put through the NSCoding protocol so that I can save the top level object into NSUserDefaults. Here is the structure of objects: 'Instructor' class NSMutableArray that holds instances…
BlueBear
  • 7,469
  • 6
  • 32
  • 47
11
votes
1 answer

Decoding with NSCoding: Does Value Exist For Key?

When using NSCoding and decoding values, is there a way to tell if a value exists for a given key? In other words, what I'm trying to do is... if([decoder valueExistsForKey:@"myKey"]) //valueExistsForKey is not a real method :( { NSInteger…
MikeS
  • 3,891
  • 6
  • 35
  • 51
10
votes
4 answers

Type Has No Subscript Members?

I get the error "Type 'Ship' has no subscript members when I try to do: var coor = ship[index] I tried to do var coor = ship?[index] as? Coordinate But I get this error: "Cannot use optional chaining on non-optional value of type 'Ship'" Here's my…
Mr_CryptoPrime
  • 628
  • 2
  • 11
  • 25
10
votes
1 answer

How do I return a pre-existing Core Data object at NSCoding initialization in Swift?

When an instance of my class is initialized using NSCoding, I want to replace it with an existing object in the Core Data database instead of calling: super.init(entity: ..., insertIntoManagedObjectContext: ...) as that would insert a new object…
Andrew
  • 15,357
  • 6
  • 66
  • 101
10
votes
4 answers

Saving PFObject NSCoding

My Problem: saveInBackground isn't working. The Reason It's not working: I'm saving PFObjects stored in an NSArray to file using NSKeyedArchiving. The way I do that is by implementing NSCoding via this library. For some reason unknown to me, several…
Apollo
  • 8,874
  • 32
  • 104
  • 192
10
votes
4 answers

Strange behavoir when decoding an NSArray via NSSecureCoding

i spent all afternoon banging my head against the wall trying to figure out why decoding of this class was failing. the class has a property that is an NSArray of Foo objects. Foo conforms to NSSecureCoding, and i have successfully encoded and…
Ben H
  • 3,855
  • 1
  • 26
  • 33
1
2
3
40 41