苹果的Swift
documentation说
If you are working with the Foundation framework in Cocoa or Cocoa Touch,the entire NSString API is available to call on any String value you create
如果我有一个String对象eg
var newString: String = "this is a string"
我如何执行NSString操作,如我的String var的containsString?
经过一些研究,看起来containsString不是一个String函数,但可以通过桥接到一个NSString访问。
原文链接:https://www.f2er.com/swift/320898.html在苹果的文档Using Swift with Cocoa and Objective-C,它说
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
但是似乎只有一些NSString的函数是可访问的,没有显式桥接。要桥接到NSString并使用其任何函数,以下方法工作:
//Example Swift String var var newString:String = "this is a string" //Bridging to NSString //1 (newString as NSString).containsString("string") //2 newString.bridgeToObjectiveC().containsString("string") //3 NSString(string: newString).containsString("string")
所有这三个工作。有趣的是,只有一些NSString方法可用于Strings,而其他方法需要显式桥接。这可能是Swift开发的基础。