Home > Software engineering >  How do I use the new Swift 5.7 String methods?
How do I use the new Swift 5.7 String methods?

Time:09-14

Is there an import or something I have to use? In my macOS app, I'm trying:

      let hello = "Hello"
      let emptyRange = hello.firstRange(of: "")

Which is an example from swift evolution proposal 0357 , which is included in swift 5.7 according to: swift.org 5.7 release notes

I've confirmed that I am using swift 5.7 by running:

xcrun swift -version

and in code via

#if swift(>=5.7)
print("on swift>=5.7")
#endif

the error I get when I try to compile the emptyRange code above is:

Value of type 'String' has no member 'firstRange'

It seems like from the example it should just work - any tips to what I'm missing would be appreciated.

CodePudding user response:

Your code works fine for me in an Xcode iOS project; for example:

    let result = "hello".firstRange(of: "")

That compiles and runs as expected. The problem seems to be that this same method hasn't work its way over to your macOS yet. If you look in the header, you'll see:

@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)

I deduce that you're not running macOS 13.

  • Related