Skip to content

Instantly share code, notes, and snippets.

@andrewgrant
Created June 30, 2015 03:42
Show Gist options
  • Save andrewgrant/a4c076c9e3664f8e6e5f to your computer and use it in GitHub Desktop.
Save andrewgrant/a4c076c9e3664f8e6e5f to your computer and use it in GitHub Desktop.
Encoding a CGColor in Swift via converting an UnsafePointer to an array
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