Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of bridgeToObjectiveC() in swift

Tags:

ios

swift

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?

like image 950
Anil Varghese Avatar asked Jun 04 '14 14:06

Anil Varghese


1 Answers

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.

like image 168
Jiaaro Avatar answered Oct 21 '22 00:10

Jiaaro