In Swift, I would expect the following to be a perfectly valid code:
let snailCharacter: Character = "đ"
let snailString = snailCharacter as String
But apparently, it produces an error:
Cannot convert value of type 'Character' to type 'String' in coercion
Solution to this is to use String
initializer like this:
let snailString = String(snailCharacter)
I thought Character
is kind of a subset of String
, so that surprised me. Why is it forbidden to cast Character
to String
?
I'm using Swift 4 in Xcode 9 beta 4.
Looking at the documentation, you can see, that Character
is just one of the several representations of String
in Swift
(emphasis added to the relevant parts by me)
A string is a series of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values.
In Swift
, String
is not just an array of Character
s unlike in some other languages. In Swift
, Character
is just a way to represent a String
instance in a certain way. String
s can be represented using View
s, such as CharacterView
, utf8View
, etc.
One of the key principles behind the architecture of Swift
's String
type was Unicode correctness, which is one of the reasons String
s are not just simply an array of Character
s.
For more information about the changes to String
in Swift4
, see the String Manifesto.
To be more specific about why casting doesn't work. There are two kinds of castings, type casting and bridge-casting. Type casting is only possible between classes, where inheritance is involved. You can either upcast a subclass to its superclass, which always succeeds or you can try to downcast a superclass to a subclass, which only works if a subclass instance was first upcasted to its superclass.
It should be quite clear from the above explanation why type casting doesn't work between Character
and String
, since the neither of the two types inherit from each other.
For bridge casting, this is a method Apple introduced for interoperability between some Swift
and Foundation
types, such as String
and NSString
, but since both String
and Character
are Swift
types, bridge casting has nothing to do with this problem either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With