6

I believe I've run into a Swift versioning issue but I'm not too sure. The code works in my Xcode platform but not in an online swift compiler. Has anyone else run into this issue or know what I can use to replace the following lines where I check for a character:

if i == 0 || !((line.range(of: ":") != nil)) 

Here is my code:

import Foundation



func hackTheString(line: String){

        var maxOpen: Int = 0
        var minOpen: Int = 0

        minOpen = 0
        maxOpen = 0
        let i = 0
        while i < line.characters.count {
            for character in line.characters {
                if character == "(" {
                    maxOpen += 1
                    if i == 0 || !((line.range(of: ":") != nil)) {
                        minOpen += 1
                        }
                    }
                else if character == ")"{
                    minOpen = max(0,minOpen-1)

                    if i == 0 || !((line.range(of: ":") != nil)){
                        maxOpen -= 1;
                        }
                    if maxOpen < 0{
                        break
                    }
                }
            }

        if maxOpen >= 0 && minOpen == 0{
            print("YES")
            }else{
                print("NO")
            }
        }
    }


while let line = readLine() {


    print(hackTheString(line))

}

The error given from the online compiler is:

source.swift:17:37: error: value of type 'String' has no member 'range'
                    if i == 0 || !((line.range(of: ":") != nil)) {
                                    ^~~~ ~~~~~
source.swift:24:37: error: value of type 'String' has no member 'range'
                    if i == 0 || !((line.range(of: ":") != nil)){
                                    ^~~~ ~~~~~
6
  • 1
    Does the online service you're using give a specific error? If it does please include that in your question. Commented Jan 7, 2017 at 2:34
  • Sorry about that @MathewS Commented Jan 7, 2017 at 2:35
  • 2
    Sounds like your online compiler is using Swift 2. Try `line.rangeOfString(":"). Commented Jan 7, 2017 at 2:50
  • 1
    And why do you have !((line.range(of: ":") != nil)) ? That's the same as line.range(of: ":") == nil. Why double-negative? Why the extra set of parentheses? Commented Jan 7, 2017 at 2:52
  • @rmaddy thanks I'll try that and Xcode made the suggestion and I was so eager to see the error go away to try testing it. Right now I'm scratching my head to find out where I went wrong for it to return my answer over and over. Commented Jan 7, 2017 at 2:56

1 Answer 1

7

I tried using range(of:) function on IBM's online swift compiler and I made sure to write import Foundation and it totally worked. Then I tried to see what version of swift is that online compiler using with following code:

#if swift(>=3.0)
print("Hello, Swift 3!")
#elseif swift(>=2.2)
print("Hello, Swift 2.2!")
#elseif swift(>=2.1)
print("Hello, Swift 2.1!")
#endif

That way I got to know that the online compiler I tried was using Swift 3 and hence the function worked perfect. Try checking what version your online compiler is using and use the related function specific to that version.

Other thing that you can do for now is use the characters array of your string and find the Index of your character. So your code might look like this:

if i == 0 || !((line.characters.index(of: ":") != nil)) {
                        minOpen += 1          
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @The Rohan Sanap however it won't allow me to check for the Swift version. When I make the recommended changes it produces a new error that reads: "error: value of type 'String.CharacterView' has no member 'index'". I do have import Foundation in place as well
@user6510422 Form what you said in comment above, I suspect your compiler uses Swift 2.x. Try using function line.characters.indexOf(":")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.