What does bridgeToObjectiveC() will do in swift. It seems like converting a Swift
object into ObjectiveC
object. Is this same as type casting?
For example i can convert a swift String
into NSString
like
var swingString:String = "test string"
var correspondingNSString = swingString.bridgeToObjectiveC() // Using bridge
var correspondingNSString = swingString as NSString // type casting
I want to know both are same or not?
My understanding is that bridgeToObjectiveC()
is provided so that you don't necessarily need to know what the corresponding objective-c type is; the compiler decides what the appropriate type is.
For example: if you have an array of Any
objects can you call bridgeToObjectiveC()
on each of them with map
without worrying about whether there are String, Int, and Double values.
Using the as
keyword allows you to explicitly choose which type you want to cast the value as.
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