ios 如何将 Swift String 桥接到 Objective C NSString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28567725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:51:41  来源:igfitidea点击:

How to bridge Swift String to Objective C NSString?

iosstringswift

提问by Bren

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.

“Swift 会自动在 String 类型和 NSString 类之间架起桥梁。这意味着在任何使用 NSString 对象的地方,您都可以改用 Swift String 类型,并获得两种类型的好处——String 类型的插值和 Swift 设计的 API 以及 NSString 类的广泛功能。因此,您几乎不需要在自己的代码中直接使用 NSString 类。事实上,当 Swift 导入 Objective-C API 时,它会将所有 NSString 类型替换为 String 类型。当您的 Objective-C 代码使用 Swift 类时,导入器会将导入的 API 中的所有 String 类型替换为 NSString。

To enable string bridging, just import Foundation.”

要启用字符串桥接,只需导入 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?

我错过了什么吗?

采纳答案by Cihan Tek

You already have the answer in your question. You're missing the cast. When writing Swift code, a statement such as this one

你的问题已经有了答案。你缺少演员。在编写 Swift 代码时,像这样的语句

var str = "Hello World"

creates a Swift String, not an NSString. To make it work as an NSString, you should cast it to an NSStringusing the asoperator before using it.

创建一个 Swift String,而不是NSString. 要使其作为 工作NSString,您应该在使用之前将其转换为NSStringusingas运算符。

This is different than calling a method written in Objective-C and supplying a Stringinstead of an NSStringas a parameter.

这与调用用 Objective-C 编写的方法并提供 aString而不是 anNSString作为参数不同。

回答by Zorayr

To go from Stringto NSStringuse the following constructor:

从去StringNSString使用下面的构造函数:

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

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

在 Xcode 7 (beta) 中,使用从Stringto向下转换NSString,如下例所示,将导致警告消息Cast from 'String?' 不相关的类型“NSString”总是失败

let objcString:NSString = swiftString as! NSString // results in error

回答by Deepak Kumar

Here is example for this :

这是一个例子:

string str_simple = "HELLO WORLD";

//string to NSString
NSString *stringinObjC = [NSString stringWithCString:str_simple.c_str()
                                encoding:[NSString defaultCStringEncoding]];            
NSLog(stringinObjC);