Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Last active April 26, 2018 03:55
Show Gist options
  • Save asmallteapot/5b9d9c1fd089a8cfa6b2b7df313c685d to your computer and use it in GitHub Desktop.
Save asmallteapot/5b9d9c1fd089a8cfa6b2b7df313c685d to your computer and use it in GitHub Desktop.
NSCoder extension for working with CodingKeys
import Foundation
import QuartzCore
extension NSCoder {
public func decodeIfPresent<T>(_ valueType: T.Type, forKey key: CodingKey) -> T? {
guard self.containsValue(forKey: key.stringValue) else { return nil }
return self.decodeObject(forKey: key.stringValue) as? T
}
public func decodeCGRectIfPresent(forKey key: CodingKey) -> CGRect? {
guard self.containsValue(forKey: key.stringValue) else { return nil }
return self.decodeCGRect(forKey: key.stringValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment