Last active
April 26, 2018 03:55
-
-
Save asmallteapot/5b9d9c1fd089a8cfa6b2b7df313c685d to your computer and use it in GitHub Desktop.
NSCoder extension for working with CodingKeys
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
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