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:
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:
func test2() - AnyClass
{
return String.self
}
Cannot convert return expression of type 'String.Type' to return type 'AnyClass' (aka 'AnyObject.Type')
So back to the ValueTransformer. This was the code that seemed to compile circa 2016. Now has a warning that it will fail.
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
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:
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.
2
0
2.9k