简体   繁体   English

类型'String'的值在Swift中没有成员'range'

[英]Value of type 'String' has no member 'range' in Swift

I believe I've run into a Swift versioning issue but I'm not too sure. 我相信我遇到了Swift版本问题,但我不太确定。 The code works in my Xcode platform but not in an online swift compiler. 代码可以在我的Xcode平台上运行,但不能在在线swift编译器中运行。 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)){
                                    ^~~~ ~~~~~

I tried using range(of:) function on IBM's online swift compiler and I made sure to write import Foundation and it totally worked. 我尝试在IBM的在线swift编译器上使用range(of:)函数,我确保编写了import Foundation ,它完全奏效了。 Then I tried to see what version of swift is that online compiler using with following code: 然后我试着看看使用以下代码的在线编译器是什么版本的swift:

#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. 这样我就知道我尝试的在线编译器使用的是Swift 3,因此功能完美无缺。 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. 您现在可以做的其他事情是使用字符串的字符数组并找到您的角色的Index So your code might look like this: 所以你的代码可能如下所示:

if i == 0 || !((line.characters.index(of: ":") != nil)) {
                        minOpen += 1          
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM