gpt4 book ai didi

arrays - 外部数组不受函数追加的影响

转载 作者:行者123 更新时间:2023-11-28 09:18:04 26 4
gpt4 key购买 nike

我正在处理数组,我创建了一个从内部追加数组的函数。但是,当我打印数组时,它仍然显示为空。给了什么?

    var queriesFinal : [String] = []

func queryValidator(search : String)
{
var letterSet = NSCharacterSet(charactersInString: "abcdefgjhijklmnopqrstuvwxyz ")
var numberSet = NSCharacterSet(charactersInString: "1234567890".uppercaseString)
var queriesTwo : [String] = search.lowercaseString.componentsSeparatedByCharactersInSet(letterSet)

for(var x = 0; x < queriesTwo.count; x++)
{
for(var y = 0; y < 10; y++)
{
var str = String(y)
if(queriesTwo[x] == str)
{
var numberStr = String(queriesTwo[x]) + "th"
queriesFinal.append(numberStr)
}
}
}
}


println(queriesFinal)

search = "Matt 8th"

queryValidator(search)

这段代码可以在 Playground 上运行..

感谢您的帮助!

最佳答案

正如 Mike S 所提到的,你犯了一个小错误 println 应该在你的 queryValidator 之后,我还添加了一个可选的以防你的 queryValidator 搜索返回 nil,同样正如 Zaph 所提到的你不需要 numberSet,所以我删除了它:

func queryValidator(search : String) -> [String]? {
let queriesTwo:[String] = search.lowercaseString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefgjhijklmnopqrstuvwxyz "))
var queriesResult:[String] = []
for x in 0...queriesTwo.count-1 {
for y in 0...9 {
if(queriesTwo[x] == String(y)) {
queriesResult.append(String(queriesTwo[x]) + "th")
}
}
}
return queriesResult.count > 0 ? queriesResult : nil
}
var search = "Matt 8 less7"
if let queriesFinal = queryValidator(search) {
println(queriesFinal)
} else {
println("no result")
}

关于arrays - 外部数组不受函数追加的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26618278/

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