Skip to content

Instantly share code, notes, and snippets.

@jk
Created February 1, 2016 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jk/eb86682c2e6797388d12 to your computer and use it in GitHub Desktop.
Save jk/eb86682c2e6797388d12 to your computer and use it in GitHub Desktop.
extension NSCoder {
// works but not optimal
func encodeProtocol<T: SequenceType>(objv: T, forKey key: String) {
let aCollection = objv.map { $0 as! AnyObject }
self.encodeObject(aCollection, forKey: key)
}
// want: func encodeObject<T: SqeuenceType where T.Element is AnyProtocol>(objv: T, forKey key: String) { ... }
func decodeObjectForKey<T>(key: String) -> [T] {
let unspecifiedCollection = self.decodeObjectForKey(key) as! [AnyObject]
return unspecifiedCollection.map { $0 as! T }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment