Is BridgeToObjectiveC really necessary?

Is it bridgeToObjectiveC()really necessary to convert between Swift Stringand NSString? What is the behavior bridgeToObjectiveC()? Since I read a lot of places, there is no need, and converting to swift Stringis NSStringimplicit.

+4
source share
3 answers

Converting a type between swift Stringis NSStringimplicit if you are importing a framework Foundation. If you use import Foundationthen there is no need to use bridgeToObjectiveC()for strings. From swift guide

Swift NSString . , , NSString, Swift String - Swift- API NSString. NSString . , Swift API Objective-C, NSString . Objective-C Swift, NSString API.

, Foundation.

,

var abcs = "somString"
abcs.length //will not work

abcs.bridgeToObjectiveC().length //you need call `bridgeToObjectiveC` explicitly

.

+3

, , . , Obj-C, String.length.

0

bridgeToObjectiveC () is not available in Swift 2.2

Use: _bridgeToObjectiveC () instead

eg:

var temp: String = "hello"
print(temp._bridgeToObjectiveC().length)
0
source

All Articles