I have a Formatter that handles value types. For the purposes of this example, let's say it is a Bool formatter:
@objc final class BoolFormatter : Formatter {
override func string(for obj: Any?) -> String? {
return (obj as? Bool) == true ? "true" : "false"
}
}It works fine. But now I want to be able to parse the Bool:
@objc final class BoolFormatter : Formatter {
override func string(for obj: Any?) -> String? {
return (obj as? Bool) == true ? "true" : "false"
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
if string == "true" {
obj?.pointee = true
return true
} else if string == "false" {
obj?.pointee = false
return true
} else {
if let error = error {
error.pointee = "String was not a boolean"
}
return false
}
}
}This fails, because Bool is not an AnyObject. Is there some special reason why Formatter can format Any (value or class), but requires AnyObject for parsing? It seems wasteful to have to wrap the value in a NSNumber or some other class.