Created
January 17, 2019 18:36
-
-
Save amosavian/b222cda827d0fbbab1148395f4acd823 to your computer and use it in GitHub Desktop.
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
extension NSCoder { | |
func encode<T: RawRepresentable>(_ object: Any?, forKey key: T) where T.RawValue == String { | |
switch object { | |
case let object as Bool: | |
encode(object, forKey: key.rawValue) | |
case let object as Int32: | |
encode(object, forKey: key.rawValue) | |
case let object as Int64: | |
encode(object, forKey: key.rawValue) | |
case let object as Float: | |
encode(object, forKey: key.rawValue) | |
case let object as Double: | |
encode(object, forKey: key.rawValue) | |
case let object as Int64: | |
encode(object, forKey: key.rawValue) | |
case let object as CGPoint: | |
encode(object, forKey: key.rawValue) | |
case let object as CGVector: | |
encode(object, forKey: key.rawValue) | |
case let object as CGSize: | |
encode(object, forKey: key.rawValue) | |
case let object as CGRect: | |
encode(object, forKey: key.rawValue) | |
case let object as CGAffineTransform: | |
encode(object, forKey: key.rawValue) | |
case let object as UIEdgeInsets: | |
encode(object, forKey: key.rawValue) | |
case let object as UIOffset: | |
encode(object, forKey: key.rawValue) | |
default: | |
encode(object, forKey: key.rawValue) | |
} | |
} | |
func decode<T: RawRepresentable>(as type: Any.Type, forKey key: T) -> Bool where T.RawValue == String { | |
return decodeBool(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: Bool.Type, forKey key: T) -> Bool where T.RawValue == String { | |
return decodeBool(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: Int.Type, forKey key: T) -> Int where T.RawValue == String { | |
return decodeInteger(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: Int32.Type, forKey key: T) -> Int32 where T.RawValue == String { | |
return decodeInt32(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: Int64.Type, forKey key: T) -> Int64 where T.RawValue == String { | |
return decodeInt64(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: Float.Type, forKey key: T) -> Float where T.RawValue == String { | |
return decodeFloat(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: Double.Type, forKey key: T) -> Double where T.RawValue == String { | |
return decodeDouble(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: CGPoint.Type, forKey key: T) -> CGPoint where T.RawValue == String { | |
return decodeCGPoint(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: CGVector.Type, forKey key: T) -> CGVector where T.RawValue == String { | |
return decodeCGVector(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: CGSize.Type, forKey key: T) -> CGSize where T.RawValue == String { | |
return decodeCGSize(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: CGRect.Type, forKey key: T) -> CGRect where T.RawValue == String { | |
return decodeCGRect(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: CGAffineTransform.Type, forKey key: T) -> CGAffineTransform where T.RawValue == String { | |
return decodeCGAffineTransform(forKey: key.rawValue) | |
} | |
#if canImport(UIKit) | |
func decode<T: RawRepresentable>(as type: UIEdgeInsets.Type, forKey key: T) -> UIEdgeInsets where T.RawValue == String { | |
return decodeUIEdgeInsets(forKey: key.rawValue) | |
} | |
func decode<T: RawRepresentable>(as type: UIOffset.Type, forKey key: T) -> UIOffset where T.RawValue == String { | |
return decodeUIOffset(forKey: key.rawValue) | |
} | |
#endif | |
@available(iOS 11.0, tvOS 11.0, macOS 10.13, *) | |
func decode<T: RawRepresentable>(as type: NSDirectionalEdgeInsets.Type, forKey key: T) -> NSDirectionalEdgeInsets where T.RawValue == String { | |
return decodeDirectionalEdgeInsets(forKey: key.rawValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment