I want to have a function return a type which I can add a new function, but still can be generally recognized as String (still have all of String's methods and still can be received by any parameter that received String).
But when I try to derive a class from String, I get this error:
Inheritance from non-protocol, non-class type 'String'
Of course, I can instead use an extension to extend the existing String to add that function, but I felt that this will pollute the String with unnecessary and unrelated functions for general use.
For example, functions that I want to add might be like this:
class ImageUrl : String {
func getImage (callback: ((UIImage?)->Void)) { ... }
}
or like this:
class Base64 : String {
var image : UIImage { ... }
var data : Data { ... }
var string : String { ... }
}
Which will be confusing if I extend these functions to the main String type.
How can I do this in Swift? Or is there any workaround to this? Thanks.
struct. So there is no way of subclassing it.Stringis a struct. Structs don't support inheritance.