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

ios – 如何将Swift String连接到Objective C NSString?,第1张

概述我吃疯狂药吗直接从文档中删除: “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 b 我吃疯狂药吗直接从文档中删除:

“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.

要启用字符串桥接,只需导入基础“.

我做了这个…考虑:

import Foundationvar str = "Hello World"var range = str.rangeOfString("e")// returns error: String does not contain member named: rangeOfString()

然而:

var str = "Hello World" as Nsstringvar range = str.rangeOfString("e")// returns correct (2,1)

我错过了什么吗?

解决方法 你已经有你的问题的答案.你错过了演员.写Swift代码时,这样一个语句
var str = "Hello World"

创建一个Swift字符串,而不是Nsstring.要使其作为Nsstring工作,您应该在使用之前使用as *** 作符将其转换为Nsstring.

这不同于调用在Objective-C中编写的方法,并提供一个String而不是一个Nsstring作为参数.

总结

以上是内存溢出为你收集整理的ios – 如何将Swift String连接到Objective C NSString?全部内容,希望文章能够帮你解决ios – 如何将Swift String连接到Objective C NSString?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1097824.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存