我有两个数组。例如:
SELECT date_format(TimeColumn, '%h:%i %p') AS TimeColumn FROM Table ORDER BY TimeColumn ASC
但是,我的firstArray = ["9,54,59,60,66,362,372,399,400,411,428"]
secondArray - ["9,40,62,399"]
let newArray = firstArray.filter { (string) -> Bool in
return secondArray.contains(string)
返回4个结果,当只有两个匹配为newArray
且40
“匹配”62
和400
时出错。
我发现了这个问题,这让我更接近,但没有解决完全匹配错误。 How to get list of common elements of 2 array in swift由RAJAMOHAN-S回答
如何将其设置为仅获得完全匹配?
更新 : 有些事情不正确。
根据以下内容,我有以下代码:
362
但是,以下是打印结果:
subGroupArrayResult.insert("[", at: subGroupArrayResult.startIndex)
subGroupArrayResult.insert("]", at: subGroupArrayResult.endIndex)
arraySpeciesIDResult.insert("[", at: arraySpeciesIDResult.startIndex)
arraySpeciesIDResult.insert("]", at: arraySpeciesIDResult.endIndex)
let newArray = subGroupArrayResult.filter { (string) -> Bool in
return arraySpeciesIDResult.contains(string)
}
print("subGroupArrayResult is \(subGroupArrayResult)")
print("arraySpeciesIDResult is \(arraySpeciesIDResult)")
print("new Array is \(newArray)")
答案 0 :(得分:3)
我在swift playground中运行了你的确切代码,并使newArray等于一个空数组。这是因为firstArray只包含一个对象String
“9,54,59,60,66,362,372,399,400,411,428”
逗号不会将字符串分解为更小的字符串或整数。引号内的所有字符都是字符串的一部分,包括逗号。
secondArray也只包含一个字符串
“9,40,62,399”
过滤firstArray测试字符串“9,40,62,399”是否包含字符串“9,54,59,60,66,362,372,399,400,411,428”,这是假的,因此newArray为空。
如果在数组的开头和结尾删除引号,则所有数字都是Ints,同样的函数返回newArray只有匹配[9,399]。
对于Int
let firstArray = [9,54,59,60,66,362,372,399,400,411,428]
let secondArray = [9,40,62,399]
let newArray = firstArray.filter { (string) -> Bool in
return secondArray.contains(string)
}
// Returns [9, 399]
对于字符串
let firstArray = ["9","54","59","60","66","362","372","399","400","411","428"]
let secondArray = ["9","40","62","399"]
let newArray = firstArray.filter { (string) -> Bool in
return secondArray.contains(string)
}
// Returns ["9", "399"]