gpt4 book ai didi

ios - stringByReplacingOccurrencesOfString 给出奇怪的错误

转载 作者:可可西里 更新时间:2023-11-01 01:05:40 25 4
gpt4 key购买 nike

出于某种原因,这给了我一个错误。swearWordPair 定义为 [String]。例如["你好", "嗨"]

让 swearWordsAndReplacements : [[String]] = [["anus", "butt"], ["arse", "butt"]]

这给我的错误是

Cannot invoke 'stringByReplacingOccurrencesOfString' with an argument list of type '(String, withString: String, options: NSStringCompareOptions, range: NSRange)

但是,我查了这个函数的参数,似乎我的所有参数都是正确的。这是一个 swift 错误吗?

var string = insult
for swearWordPair in swearWordsAndReplacements {
string = string.stringByReplacingOccurrencesOfString(swearWordPair[0],
withString: swearWordPair[1],
options: NSStringCompareOptions.CaseInsensitiveSearch,
range: NSMakeRange(0, count(string)))
}

最佳答案

Range不是 NSRange ,而是一个 Swift范围。在这种情况下,您可以通过 nil代表整个 String :

string = string.stringByReplacingOccurrencesOfString(swearWordPair[0],
withString: swearWordPair[1],
options: .CaseInsensitiveSearch,
range: nil)

如果你想限制范围,你可以创建一个Range<String.Index>像这样:

var string = "I laid my head at the head of the bed"

// Only replace within the first 15 characters
let myrange = string.startIndex ..< advance(string.startIndex, 15)

string = string.stringByReplacingOccurrencesOfString("head",
withString: "blanket",
options: .CaseInsensitiveSearch,
range: myrange)

println(string)

输出:

I laid my blanket at the head of the bed

关于ios - stringByReplacingOccurrencesOfString 给出奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998378/

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