Created
June 30, 2015 03:42
-
-
Save andrewgrant/a4c076c9e3664f8e6e5f to your computer and use it in GitHub Desktop.
Encoding a CGColor in Swift via converting an UnsafePointer to an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
required init(coder aDecoder: NSCoder) { | |
let colorArray = aDecoder.decodeObjectForKey("colors") as! [CGFloat] | |
self.CGColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), colorArray) | |
super.init() | |
} | |
func encodeWithCoder(aCoder: NSCoder) { | |
let colors = CGColorGetComponents(CGColor) | |
let colorBuffer = UnsafeBufferPointer(start: colors, count: 4) | |
let colorArray = [CGFloat](colorBuffer) | |
aCoder.encodeObject(colorArray, forKey: "colors") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment