Swift中Tuple的比较
Swift中Tuple的比较遵循如下规则:
1 被比较的Tuple中包含的元素个数必须一样,并且对应元素的类型也必须一样;
2 比较的结果由整个Tuple的比较结果来决定。比如,如果是相等比较,那么必须两个Tuple中的所有元素相等才行;
(1, "zebra") < (2, "apple") // true because 1 is less than 2; "zebra" and "apple" are not compared
(3, "apple") < (3, "bird") // true because 3 is equal to 3, and "apple" is less than "bird"
(4, "dog") == (4, "dog") // true because 4 is equal to 4, and "dog" is equal to "dog"
3 被比较的Tuple元素必须能够适用于比较操作符才行:
("blue", -1) < ("purple", 1) // OK, evaluates to true
("blue", false) < ("purple", true) // Error because < can't compare Boolean values
另一个需要注意的地方是,Swfit标准库对于Tuple的比较限制了元素个数只能少于过7个,如果等于或者超过了7个,就需要自己实现了。