Tracking down some errors in ValueTransformers in an old project. It seems that String.self can't be returned as AnyClass since it is a value type.
So this works in Xcode 12.5:
But this doesn't:
String is a struct not a class.
And doing this produces an error - because String is a struct
So is the correct approach to work with NSStrings in ValueTransformers? Simply cast the last return with:
Similarly, Bool is also a struct and has the same issues. But BOOL isn't available so it looks like NSNumber.
So this works in Xcode 12.5:
Code Block func test() -> AnyClass { // just for checking type(of: NSString.self) type(of: String.self) type(of: NSNumber.self) return NSString.self }
But this doesn't:
Code Block func test2() -> AnyClass { return String.self }
So back to the ValueTransformer. This was the code that seemed to compile circa 2016. Now has a warning that it will fail.Cannot convert return expression of type 'String.Type' to return type 'AnyClass' (aka 'AnyObject.Type')
Code Block override class func transformedValueClass() -> AnyClass { return String.self as! AnyClass }
String is a struct not a class.
And doing this produces an error - because String is a struct
Code Block override class func transformedValueClass() -> AnyClass { return String.self }
So is the correct approach to work with NSStrings in ValueTransformers? Simply cast the last return with:
Code Block return swiftNSString(utf8String:string)
Similarly, Bool is also a struct and has the same issues. But BOOL isn't available so it looks like NSNumber.