Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bridge Swift String to Objective C NSString?

Tags:

string

ios

swift

Am I taking crazy pills? Directly out of the documentation:

“Swift automatically bridges between the String type and the NSString class. This means that anywhere you use an NSString object, you can use a Swift String type instead and gain the benefits of both types—the String type’s interpolation and Swift-designed APIs and the NSString class’s broad functionality. For this reason, you should almost never need to use the NSString class directly in your own code. In fact, when Swift imports Objective-C APIs, it replaces all of the NSString types with String types. When your Objective-C code uses a Swift class, the importer replaces all of the String types with NSString in imported API.

To enable string bridging, just import Foundation.”

I've done this... consider:

import Foundation

var str = "Hello World"

var range = str.rangeOfString("e")

// returns error: String does not contain member named: rangeOfString()

However:

var str = "Hello World" as NSString

var range = str.rangeOfString("e")

// returns correct (2, 1)

Am I missing something?

like image 664
Bren Avatar asked Feb 17 '15 17:02

Bren


People also ask

How do I add a Swift file to Objective-C project?

Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .

Can you mix Objective-C and Swift?

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

How do you make a bridging header in Objective-C?

To create an Objective-C bridging header file, all you need to do is drag some Objective-C code into your Swift project – Xcode should prompt you with the message "Would you like to configure an Objective-C bridging header?" Click "Creating Bridging Header" and you'll see a file called YourProjectName-Bridging-Header.

What is difference between string and NSString in Swift?

The Swift string is one character long, as expected. The NSString says it has a length of seven — this matches with the length of the Swift string's utf16 view, since NSStrings are backed by UTF-16: 09:02 The Swift string's unicodeScalars view returns a count of four.


1 Answers

To go from String to NSString use the following constructor:

let swiftString:String = "I'm a string."
let objCString:NSString = NSString(string:swiftString)

With Xcode 7 (beta), using a downcast from String to NSString, as in below example, will result in a warning message, Cast from 'String?' to unrelated type 'NSString' always fails:

let objcString:NSString = swiftString as! NSString // results in error
like image 85
Zorayr Avatar answered Oct 05 '22 03:10

Zorayr