David David -4 years ago 194
Swift Question

Custom class archiving in Swift

I would like to archive and unarchive a custom object in swift, something like this:

class Line : NSObject, NSCoding {

var start : CGPoint

init(start _start: CGPoint) {
start = _start

required init(coder aDecoder: NSCoder) {
// 1 - compile time error
self.start = aDecoder.decodeObjectForKey("start") as CGPoint

override init() {

func encodeWithCoder(aCoder: NSCoder) {
// 2 - compile time error
aCoder.encodeObject(start, forKey: "start")

The 1. compile time error is:
Type 'CGPoint' does not conform to protocol 'AnyObject'

The 2. compile time error is:
Extra argument 'forKey' in call

How to archive and unarchive a CGPoint, I know CGPoint is a struck and this is the problem, but how to solve it?

thanks in advance.

Answer Source

You can archive/unarchive a CGPoint using :




More information here : https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCoder_Class/index.html#//apple_ref/occ/instm/NSCoder/encodeCGPoint:forKey:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download