Menu
  • HOME
  • TAGS

NSCoding data not saved when returning to the app?

ios,objective-c,nsuserdefaults,nscoding

I had to use this : if ( previousArray == nil ) ... NSMutableArray *previousArray = [[NSKeyedUnarchiver unarchiveObjectWithData:previousData] mutableCopy]; if ( previousArray == nil ) previousArray = [[NSMutableArray alloc] init]; ...

Save subclass of SKShapeNode using NSCoding

sprite-kit,nskeyedarchiver,nscoding

Since you appear to be using NSCoding to encode the nodes, you need to implement NSCoding in your subclass. Right now SKShapeNode is doing all of the encoding, but it doesn't know about your subclass attributes. To get your attributes encoded, you need to implement encodeWithCoder and initWithCoder. Both of...

I want to have two versions of encodeWithCoder in the same class

ios,swift,nscoding,nscoder

Rather than trying to have 2 versions of it you should figure out what it is that determines which encoding you want to use, and make that a separate flag/field and encode that too. func encodeWithCoder(coder: NSCoder) { coder.encodeObject(self.name, forKey: "name") if self.someFlag == true { coder.encodeObject(self.salary, forKey: "salary") }...

Saving PFObject NSCoding

ios,objective-c,parse.com,nskeyedarchiver,nscoding

I have created a very simple workaround that requires no change the above NSCoding Libraries: PFObject *tempRelationship = [PFObject objectWithoutDataWithClassName:@"relationship" objectId:messageRelationship.objectId]; [tempRelationship setObject:[NSNumber numberWithBool:YES] forKey:@"read"]; [tempRelationship saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (succeeded) NSLog(@"Success"); else NSLog(@"Error"); }]; What we're doing here is creating a temporary object with the same objectId,...

How to make a deep copy with copyWithZone to duplicate a structure?

ios,iphone,cocoa,nscoding,nscopying

Encode the parent with [coder encodeConditionalObject:self.parent forKey:@"parent"]. Decode it with -decodeObjectForKey: as normal. What this does is, if the parent would be archived for some other reason — say it was a child of an object higher in the tree — the reference to the parent is restored. However, if...

How can I get an NSCoder to encode/decode a Swift array of structs?

arrays,swift,serialization,nscoding

Here is a possible solution that encodes the UInt64 array as an array of bytes. It is inspired by the answers to How to serialize C array with NSCoding?. class MyObject: NSObject, NSCoding { var values: [UInt64] = [] init(values : [UInt64]) { self.values = values } // MARK: -...

Swift encode UInt8

arrays,swift,numbers,nscoding

encodeObject(_:forKey:) in NSCoder accept AnyObject? type: func encodeObject(_ objv: AnyObject?, forKey key: String) So if you pass an array of Int to encodeObject, it is implicitly converted to NSArray of NSNumbers. This mechanism is explained in the document: When you bridge from a Swift array to an NSArray object, the...

initWithCoder: get called without import the category

ios,objective-c,uiimageview,nscoding

You don''t need to import the the categories explicitly unless you are adding a new method to the category and using it in your controller(importing is required since the compiler would know that the method is available). Here what's happening is your Xib may contain an UIImageView, while the Xib...

Encoding, decoding, and editing a NSMutableArray with NSCoding

ios,objective-c,nsmutablearray,nscoding

My guess is _dataArray is nil. You should add code to your initWithCoder that checks to see if it was able to read array data. If not, _dataArray will be nil. In that case you should initialize an empty mutable array. Your updated initWithCoder might look like this: -(instancetype)initWithCoder:(NSCoder *)decoder...

NSKeyedArchiver would not save or load objects

ios,objective-c,nskeyedarchiver,nscoding,nskeyedunarchiver

unarchiveObjectWithFile: returns the unarchived object for you to use. At the moment you just check if it is nil (with your if statement) and then it gets destroyed (deallocated because you don't use it, assuming you're using ARC). So, get the object returned by unarchiveObjectWithFile: and set it to your...

How to initwithCoder and encodewithCoder to get value of pointer

objective-c,pointers,nscoding

If you look at the docs of the related method decodeBytesWithReturnedLength:, it says: If you need the bytes beyond the scope of the current @autoreleasepool block, you must copy them. I assume that decodeBytesForKey:returnedLength: follows the same rules. So your code should look like this: - (id)initWithCoder:(NSCoder *)decoder { NSUInteger...

NSKeyedArchiver saving iOS

ios,objective-c,nscoding

Loading data should be "lazy". This means the data should be loaded the first instant that you actually need to read the data. Also, if it's a lot of data, you should be prepared to free it while your app is running so other apps can use the RAM, this...

NSUserdefaults not working with NSKeyedArchiver

ios,objective-c,nsuserdefaults,nscoding,nskeyedarchiver

Your Movie initWithName... method is incorrect. It needs to be: - (instancetype)initWithName:(NSString *)newName year:(int)newYear length:(int)newLength filesize:(NSString *)newFileSize rating:(int)newRating genre:(NSArray *)newGenre plot:(NSString *)newPlot { self = [super init]; if (self) { self.name = newName; self.year = newYear; self.length = newLength; self.file_size = newFileSize; self.rating = newRating; self.genre = newGenre; self.plot =...

Xcode NsCoding navigation title disappears

xcode,crash,title,nscoding,initwithcoder

Don't call [super init]. Call [super initWithCoder:] instead.

Swift NSCoding with NSValue

ios,swift,nscoding,nsvalue

I didn't even get as far as you did. I crashed on just these two lines: let loc = CLLocationCoordinate2D(latitude: 20, longitude: 20) let val = NSValue(MKCoordinate:loc) This tells me that NSValue(MKCoordinate:) is broken. And it's not a Swift issue; I get the same crash using the same code translated...

How can data be stored on an iPhone/iPad based on data volume?

ios,ipad,core-data,data-storage,nscoding

Opinion: CoreData is the way to go. Yes, there is a learning curve - but it's also really pretty awesome. Once you get the hang of it, you will find that it's really a powerful tool and will allow you to tackle more complicated scenarios as you advance. Images can...

Using NSCoder and NSKeyedArchiver with runtime reflection to deep copy a custom class

objective-c,reflection,nskeyedarchiver,nscoding

Did you try: object_setInstanceVariable object_setIvar https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ObjCRuntimeRef/ You might want to be calling class_copyIvarList instead of the property version - just getting properties might miss IVars depending on your use case. Also does your super class adopt NSCoding? You'll have to call the super implementations of initWithCoder and encodeWithCoder. Note also...

Swift encode tuple using NSCoding

swift,tuples,nscoding

Tuple cannot be encoded because it is not a class, but one approach is to encode each component of a tuple separately and then upon decoding you decode each component and then set the value of the tuple to a tuple constructed from the decoded content. class ViewController: UIViewController {...

Error with encodeWithCoder

ios,google-maps,core-data,google-maps-sdk-ios,nscoding

GMSMarker does not respond to encodeWithCoder:, so you can't put it into a dictionary that will then be encoded (which is what happens when you set a dictionary as an attribute in core data). You should just be storing the underlying data waypoints in core data and then rebuilding the...

How to encode and decode this class

swift,nscoding

How about something like this?: class ShoppingList: NSObject, NSCoding { var typeOne: [Type]! var typeTwo: [Type]! var typeThree: [Type]! override init() { super.init() } required init(coder aDecoder: NSCoder) { let nils = aDecoder.decodeObjectForKey("nils") as [Bool] if nils[0] { typeOne = nil } else { let typeOneInt = aDecoder.decodeObjectForKey("typeOneInt") as [Int]...

Changing from NSUserDefaults to NSCoding

ios,swift,sprite-kit,nsuserdefaults,nscoding

The class you want to save has to conform to the NSCoding protocol and implement required init(coder aDecoder: NSCoder) and func encodeWithCoder(aCoder: NSCoder). Here's a simplified example: class GameState: NSObject, NSCoding { var score: Int? var gameOver: Bool? init(score: Int, gameOver: Bool) { super.init() self.score = score self.gameOver = gameOver...

NSCoding - saving array to file or nsdefaults

ios,arrays,nscoding

You are trying to hard. If what you want to save is just NSArrays and NSStrings to you do not need so add an NSCoding, these types already conform to NSCoding. Just Archive to a file or "shudder" save to NSUserDefaults "/shudder". It is really better to create a Data...

How to migrate an object which has been persisted with NSKeyedArchiver?

ios,migration,nscoding,nskeyedarchiver,object-persistence

Many Thanks to Mike Mayo (@greenisus) the original author of Archiver for the clue to what was going on... It turns out that during decoding it is possible to check for the existence of any given key. The Archiver project uses the function below to decode objects; so at the...

Converting CLLocationCoordinate2D to a String that can be stored

ios,swift,location,cllocation,nscoding

This way you can store Locations to NSUserDefaults: //First Convert it to NSNumber. let lat : NSNumber = NSNumber(double: Location.latitude) let lng : NSNumber = NSNumber(double: Location.longitude) //Store it into Dictionary let locationDict = ["lat": lat, "lng": lng] //Store that Dictionary into NSUserDefaults NSUserDefaults.standardUserDefaults().setObject(locationDict, forKey: "Location") After that you can...