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)){
^~~~ ~~~~~
!((line.range(of: ":") != nil))? That's the same asline.range(of: ":") == nil. Why double-negative? Why the extra set of parentheses?