2017-06-05 58 views
-2

我无法弄清楚如何让数组的一部分匹配整个数组,并且我想假装我们不知道usersInData [0,1,2,3]与verifiedUsers [0,1,2,3]相同,我想以某种方式匹配相同的值,而不使用每个数组的索引。数组中的某些字符串与另一个数组匹配

我想这 -

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"] 

var verifiedUsers = ["hello", "hello1", "hello2", "hello3"] 

if usersInData == verifiedUsers { 
    print("special user") 
} else { 
    print("regular user") 
} 

但它打印“普通用户”,所以基本上没有工作。 我希望验证的用户与常规用户不同。因此,例如用户“你好”得到一个特殊的验证图标等。谢谢!

+0

可能重复的https://stackoverflow.com/questions/39161168/how-to-compare-two-array-of-objects和https://stackoverflow.com/questions/36714522/how-do -i-check-in-swift-if-two-arrays-contain-the-same-elements-whatever-th- –

+1

正如我从你的问题中可以理解的那样,你试图找出共同的价值。对?如果是,则从给定的两个数组创建两个集合并查找它们的交集。相交集将返回数组。例如let fruitsSet = Set(firstArray) let vegSet = Set(secondArray) let output = Array(fruitsSet.intersect(vegSet)) –

回答

2

是这样的,你在追求什么?

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"] 

var verifiedUsers = ["hello", "hello1", "hello2", "hello3"] 

for user in usersInData { 
    if verifiedUsers.contains(where: { $0 == user }) 
     print("\(user) is a special user") 
    } else { 
     print("\(user) is a regular user") 
    } 
} 
0

您必须使用contains:func contains(_ element:String) - > Bool。不幸的是,这个函数的默认实现只比较一个元素。

你必须扩展数组并创建func containsArray(array:[Element]) - > Bool。

让我们开始吧:

extension Array where Element: Equatable { 
    func containsArray(array: [Element]) -> Bool { 
     return !array.contains { !self.contains($0) } 
    } 
} 

如何在代码中使用它:

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"] 
var verifiedUsers = ["hello", "hello1", "hello2", "hello3"] 


if usersInData.containsArray(array: verifiedUsers) { 
    print("Special user") 
} else { 

    print("Regular user") 
} 
1

我不太清楚你找什么在这里。

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"] 
var verifiedUsers = ["hello", "hello1", "hello2", "hello3"] 

如果你想知道,如果verifiedUsersusersInData有共同的任何元素。

if !Set(verifiedUsers).isDisjoint(with: usersInData) { 
    print("special user") 
} else { 
    print("regular user") 
} 

如果你想知道的话,verifiedUsers每一个元素是usersInData

if Set(verifiedUsers).isSubset(of: usersInData) { 
    print("special user") 
} else { 
    print("regular user") 
} 

if Set(usersInData).isSuperset(of: verifiedUsers) { 
    print("special user") 
} else { 
    print("regular user") 
} 

如果你想知道,如果usersInData包含subse那么verifiedUsers,那么这有点欺骗。

for i in usersInData.startIndex ..< usersInData.endIndex - verifiedUsers.count { 
    if verifiedUsers.elementsEqual(usersInData[i ..< i + verifiedUsers.count]) { 
     print("special user") 
     break 
    } 
} 

我相信对后续问题有更好的答案。

UPDATE

通过@iLandes API的启发,我说我的包测试序列。

extension Array where Element: Equatable { 
    func hasSubsequence(_ other: Array) -> Bool { 
     for i in startIndex ..< endIndex - other.count { 
      if other.elementsEqual(self[i ..< i + other.count]) { 
       return true 
      } 
     } 

     return false 
    } 
} 

if usersInData.hasSubsequence(verifiedUsers) { 
    print("special user") 
} else { 
    print("regular user") 
} 
0

我知道你想知道哪个值出现在两个数组中。

我们可以使用Sets找到它们,从​​在数学中已知。

你在找什么叫做交叉口 - 在这两组元素中都有一个元素。数学符号:A ∩ B

let usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"] 
let verifiedUsers = ["hello", "hello1", "hello2", "hello3", "hello1212"] 
let verifiedUsersInData = Array(Set(usersInData).intersection(verifiedUsers)) 
print(verifiedUsersInData) 

输出:

["hello3", "hello", "hello2", "hello1"] 

,你可以本身在结果阵列具有比两个输入数组另一顺序。这是因为套件没有排序的概念。

如果维持秩序是对你很重要,你有几种选择:

  • 由顺序比较输入数组的一个结果数组排序,
  • 包裹在输入数组的值具有权重值的类型,通过它的排序结果数组,比解开,
  • 使用NSOrderedSet,这使订单,但没有工作迅速类型,
  • 找到一个有序的设置实行网上。我没有任何建议,但你应该选择一个承诺与我们使用的BigO特性相同的BigO特性。
0
var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"] 
    var verifiedUsers = ["hello", "hello1", "hello2", "hello3"] 

    for user in usersInData { 
     if verifiedUsers.contains(user){ 
     print("\(user) is a special user") 
    } else { 
     print("\(user) is a regular user") 
    } 
    } 

试试这个。它会工作

相关问题