gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:15:31 25 4
gpt4 key购买 nike

我正在尝试查找字符“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
}

我收到错误消息:二元运算符“<”不能应用于两个“String.CharacterView.Index?” (又名“可选”)操作数

根据我使用 swift 的经验,这通常意味着其他错误。我还尝试将 if 语句更改为下面的内容。

if (C == nil) || S?.encodedOffset < C?.encodedOffset {
nextView.sequential_flag = true
nextView.continuous_flag = false
}

我收到错误消息:二元运算符“<”不能应用于两个“Int?”操作数。

非常感谢任何有关如何执行此操作的帮助,谢谢。

最佳答案

您应该检查 S 是否为 nil 并向 C 提供一个回退值。然后你可以比较两个非可选值。

if let S = S, S.encodedOffset < (C?.encodedOffset ?? Int.max) {
nextView.sequential_flag = true
nextView.continuous_flag = false
}

关于swift - 如何比较swift 4中的两个字符串索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com