我该如何检查,如果元组数组包含斯威夫特的特定的一个? [英] How do I check if an array of tuples contains a particular one in Swift?

查看:118
本文介绍了我该如何检查,如果元组数组包含斯威夫特的特定的一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下雨燕code。

Consider the following Swift code.

var a = [(1, 1)]

if contains(a, (1, 2)) {
    println("Yes")
}

所有我需要的是检查 A 包含元组,但code会导致错误。

All I need is to check if a contains the tuple but the code leads to error.

有关'包含'接受一个参数列表中找不到该超载
  类型([(INT,INT),(INT,INT))'的

Cannot find an overload for 'contains' that accepts an argument list of type '([(Int, Int)], (Int, Int))'

为什么这么以及如何使用包含是否正确?

Why so and how to use contains properly?

推荐答案

以下内容添加到您的code:

Add the following to your code:

func contains(a:[(Int, Int)], v:(Int,Int)) -> Bool {
  let (c1, c2) = v
  for (v1, v2) in a { if v1 == c1 && v2 == c2 { return true } }
  return false
}

雨燕是不是灵活,当涉及到元组。他们不符合 Equatable 的协议。所以,你必须定义一个或使用上述功能。

Swift is not that flexible when it comes to tuples. They do not conform to the Equatable protocol. So you must define that or use the above function.

这篇关于我该如何检查,如果元组数组包含斯威夫特的特定的一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆