HelloGameKit WatchKit Extension/Move.swift

/*
    Copyright (C) 2016 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information
    
    Abstract:
    A move is one or more points recorded by a gesture
*/
 
import UIKit
 
class Move: NSObject, NSCoding {
    // MARK: Properties
    
    private(set) var points = [CGPoint]()
    
    let playerID: String
    
    // MARK: Initialization
    
    init(playerID: String) {
        self.playerID = playerID
        
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        let pointStrings = aDecoder.decodeObject(forKey:"points") as! [String]
        
        points = pointStrings.map { CGPointFromString($0) }
        playerID = aDecoder.decodeObject(forKey:"playerID") as! String
    }
    
    // MARK: NSCoding
    
    func encode(with aCoder: NSCoder) {
        let pointStrings = points.map { NSStringFromCGPoint($0) }
        aCoder.encode(pointStrings as NSArray, forKey: "points")
        aCoder.encode(playerID as NSString, forKey: "playerID")
    }
    
    // MARK: Public Accessors
    
    func add(position: CGPoint) {
        points.append(position)
    }
}