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
10
votes
6 answers

Inspecting files of type "NeXT/Apple typedstream" version 4 (NSArchiver)

For a data recovery program I need to be able to extract the values+types from files written by NSArchiver, without having access to Apple's CF / NS frameworks. The OS X file command reports such files as: NeXT/Apple typedstream data, little endian,…
Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
10
votes
1 answer

How to use NSCoding protocol with an enum?

i have this enum: typedef types { HBIntineraryTypeVisited = 0, HBIntineraryTypeUnvisited, HBIntineraryTypeUnknown, HBIntineraryTypeDeleted, } HBIntineraryType; and want to store it along with some other variables using the nscoding…
bogen
  • 9,954
  • 9
  • 50
  • 89
10
votes
4 answers

Encoding a CLLocationcoordinate2D

I am trying to encode annotations that are on a map, but I read that I am not able to encode CLLocationcoordinate2D variables. Does anyone know how I can solve this? Here is some code. This is where I drop the pins: -…
Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45
9
votes
1 answer

Does not conform to protocol 'NSCoding' - Swift 3

I have seen several questions similar to mine; however, those are pertaining to swift 2/1 and I am currently using swift 3. I believe Apple has changed it slightly. class Person: NSObject, NSCoding { var signature: UIImage init(signature:…
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
9
votes
1 answer

How to store an MKPolyline attribute as transformable in IOS coredata with swift?

What would be the code required to allow the storage of an MKPolyline in CoreData in swift. So for example if I had one of my core data entities (say "myEntity") for which I wanted to save an MKPolyline, and have added the "polyline" field as…
Greg
  • 34,042
  • 79
  • 253
  • 454
9
votes
2 answers

Swift encode tuple using NSCoding

Is it possible to store a tuple using NSCoding? I have a tuple like ((UInt8, UInt8), (UInt8, UInt8)). But aCoder.encodeObject(myTuple) doesn't work. Do I have to convert the tuple into NSData or is this absolutely not possible? Thanks for any help
borchero
  • 5,562
  • 8
  • 46
  • 72
9
votes
3 answers

Swift call class function from corresponding subclass in superclass function

I would like to implement init(coder aDecoder: NSCoder!) in a superclass, and use it in all subclasses by calling a class method on the particular subclass in the superclass at runtime. MySuperClass class func dummyDict() ->…
the_critic
  • 12,720
  • 19
  • 67
  • 115
9
votes
2 answers

iOS - EncodeWithCoder - Encode Nil

I have an object that has a property that might be nil. How should I implement this in encodeWithCoder (and decodeWithCoder)? - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_duration forKey:kDuration]; //_duration could be…
rizzes
  • 1,532
  • 20
  • 33
9
votes
2 answers

NSUserDefaults vs NSCoding

When saving small amounts of data from within my App is it better to use NSUserDefaults or NSCoding? Right now I use NSCoding (encodeWithCoder/initWithCoder, etc.) but it appears that NSUserDefaults might be simpler. My total data is about a variety…
wayneh
  • 4,393
  • 9
  • 35
  • 70
8
votes
1 answer

What happens to an NSArray object when encoding?

I'm building an application that utilises NSCoding to save NSObject's to a documentPath. I'm having no issues doing this, I'm just curious about something. I have MACompany, which implements NSCoding delegate methods. - (void)…
Sebastien Peek
  • 2,528
  • 2
  • 23
  • 32
8
votes
1 answer

NSCoding with as NSString inside a object

My issue is then I retrieve my NSArray of Store objects, all my NSString properties are causing BadAccess errors. The int and double properties work fine! store.h @interface Store : NSObject { NSString *Name; NSString *Address; …
8
votes
4 answers

Save struct in class to NSUserDefaults using Swift

I have a class and inside the class is a (swift) array, based on a global struct. I want to save an array with this class to NSUserDefaults. This is my code: struct mystruct { var start : NSDate = NSDate() var stop : NSDate =…
Thomas
  • 1,468
  • 4
  • 14
  • 20
7
votes
1 answer

How to unit test NSCoding?

I have an iOS application with data persisted using NSCoding and more precisely NSKeyedArchiver. This application is already available on the App Store. I'm working on version 2 of the application and the data model should change. So I need to…
David
  • 998
  • 7
  • 17
7
votes
1 answer

How to encode and decode a custom class with NSKeyedArchiver

I have a custom class that I wish to save and load. The class contains an NSDate, an NSString, and an NSNumber. I have implemented the NSCoding protocol in the .h file. Here is the code I have so far. theDate is an NSDate. theName is the NSString.…
7
votes
4 answers

Unarchiving encoded data returning nil and incorrect format

I have added category methods to NSUserDefaults to store and retrieve encoded objects (in this case, an NSArray). I am having a problem retrieving the encoded data. Here is my code: - (void)encodeObject:(id)object forKey:(NSString *)key { …
mashers
  • 1,009
  • 7
  • 22
1 2
3
40 41