Created
March 21, 2018 16:11
-
-
Save juliengdt/ce062fcd2c8bca316825f3f38b3167d1 to your computer and use it in GitHub Desktop.
Because NSObject KV setter sucks by default
This file contains hidden or 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 NSObject { | |
private enum SetValuableErrorType: Error, CustomStringConvertible { | |
case unknown(key: String, type: NSObject) | |
var description: String { | |
switch self { | |
case .unknown(key: let k, type: let t): | |
return "Unknown parameters \(k) for object typped \(String(describing:t))" | |
} | |
} | |
} | |
func set(value: Any?, forKey key: String) throws { | |
guard let _ = self.value(forKey: key) else { | |
throw SetValuableErrorType.unknown(key: key, type: self) | |
} | |
self.setValue(value, forKey: key) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment