如何比较swift 4中的两个字符串索引

如何比较swift 4中的两个字符串索引,swift,string,swift4,Swift,String,Swift4,我试图找出字符“S”或“C”是否出现在字符串中。字符串中至少有一个,但不一定两者都有 let S = codeZ.characters.index(of: "S") let C = codeZ.characters.index(of: "C") if (C == nil) || S < C { nextView.sequential_flag = true nextView.continuous_flag =

我试图找出字符“S”或“C”是否出现在字符串中。字符串中至少有一个,但不一定两者都有

let S = codeZ.characters.index(of: "S")
        let C = codeZ.characters.index(of: "C")

        if (C == nil) || S < C {
            nextView.sequential_flag = true
            nextView.continuous_flag = false
        }
        else if S == nil || (C < S) {
            nextView.sequential_flag = false
            nextView.continuous_flag = true
        }
让S=codeZ.characters.index(of:“S”)
设C=codeZ.characters.index(of“C”)
如果(C==nil)| S

我得到一个错误:二进制运算符“您应该检查
S
是否为
nil
,并向
C
提供一个回退值。然后可以比较这两个非可选值

if let S = S, S.encodedOffset < (C?.encodedOffset ?? Int.max) {
    nextView.sequential_flag = true
    nextView.continuous_flag = false
}
如果设S=S,则S.encodedOffset<(C?.encodedOffset??Int.max){
nextView.sequential_标志=真
nextView.continuous_标志=false
}

谢谢,但我能问一下当C为零而S不是时会发生什么情况吗?我仍然需要它通过。一件简单的事情,可选值不应该像9999而不是0,以确保通过吗?否则,感谢您的快速响应。这取决于回退案例所需的比较结果。如果希望
C
为nil时为true,请使用
Int.max
作为回退值。
if let S = S, S.encodedOffset < (C?.encodedOffset ?? Int.max) {
    nextView.sequential_flag = true
    nextView.continuous_flag = false
}