How do I check if a string contains another string in Swift?

后端 未结 27 3648
天命终不由人
天命终不由人 2020-11-22 12:32

In Objective-C the code to check for a substring in an NSString is:

NSString *string = @\"hello Swift\";
NSRange textRange =[strin         


        
27条回答
  •  时光取名叫无心
    2020-11-22 13:25

    You can just do what you have mentioned:

    import Foundation
    ...
    string.contains("Swift");
    

    From the docs:

    Swift’s String type is bridged seamlessly to Foundation’s NSString class. 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, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.

    You need to import Foundation to bridge the NSString methods and make them available to Swift's String class.

提交回复
热议问题