Why can#39;t I use a tuple constant as a case in a switch statement(为什么我不能在 switch 语句中使用元组常量作为案例)
问题描述
我决定使用 Swift 案例语句和元组.它看起来像是该语言最酷的功能之一.
I decided to play with Swift case statements and tuples. It looks like one of the cooler features of the language.
我决定使用月/日/年元组.令我惊讶的是,我不能在 switch 语句中使用常量元组值作为 case.这是一个示例(可以粘贴到 Playground 中并运行)
I decided to play with month/day/year tuples. To my surprise, I can't use a constant tuple value as a case in a switch statement. Here is an example (can be pasted into a Playground and run)
import UIKit
typealias mdyTuple = (month: Int, day: Int, year: Int)
let joesBirthday: mdyTuple = (month: 6, day: 7, year: 1978)
let someday: mdyTuple = (6, 7, 1978)
switch someday
{
//---------
//The line "case joesBirthday" won't compile.
//case joesBirthday:
// println("Joe was born on this day"
//---------
case (joesBirthday.month, joesBirthday.day, joesBirthday.year):
println("Joe was born on this day")
case (joesBirthday.month, joesBirthday.day, let year):
println("Joe is (year-joesBirthday.year) today")
default:
println("Some other day")
}
注释掉的代码 case joesBirthday:
将无法编译(在 Xcode 6.3 中,如果重要的话).下面的例子(我分别列出了 joesBirthday 元组的所有元素)既难以输入,又难以阅读,确实有效)
The commented out code, case joesBirthday:
, will not compile (in Xcode 6.3, if that matters). The case below (where I list all the elements of the joesBirthday tuple separately) which is both harder to type, and harder to read, does work)
我的 Playground 在键入此内容时使 Xcode 崩溃,并在尝试重新启动 Xcode 时再次崩溃,因此我无法报告错误代码.
My Playground crashed Xcode when typing this up, and crashed AGAIN trying to restart Xcode, so I'm having trouble reporting the error code.
好的,我终于让 Xcode 停止崩溃(在连续 4 次崩溃之后.耶耶!)错误是二进制运算符 ~=
不能应用于两个 mdyTuple 操作数."
Ok, I finally got Xcode to stop crashing (after 4 crashes in a row. Yayyy!) The error is "Binary operator ~=
cannot be applied to two mdyTuple operands."
为什么要尝试使用 ~=
操作数?元组不是相等的吗?
Why is it trying to use the ~=
operand? Aren't like tuples equatable?
是否有一些 clean 替代语法可以让我在 switch 语句的情况下使用常量元组?
Is there some clean alternative syntax that lets me use a constant tuple in a case of a switch statement?
推荐答案
您可以像这样为 mydTuple
类型实现 ~=
运算符:
You could implement the ~=
operator for the mydTuple
type like this:
func ~=(a: mdyTuple, b: mdyTuple) -> Bool {
return a.month ~= b.month && a.year ~= b.year && a.day ~= b.day
}
这在操场上对我有用...现在,这段代码
That worked for me in a Playground... Now, this code
switch someday {
case joesBirthday:
println("one")
default:
println("two")
}
打印一".
这是运算符的定义:
infix operator ~= {
associativity none
precedence 130
}
并针对以下情况实施:
/// Returns `true` iff `pattern` contains `value`
func ~=<I : IntervalType>(pattern: I, value: I.Bound) -> Bool
func ~=<T>(lhs: _OptionalNilComparisonType, rhs: T?) -> Bool
func ~=<T : Equatable>(a: T, b: T) -> Bool
func ~=<I : ForwardIndexType where I : Comparable>(pattern: Range<I>, value: I) -> Bool
这篇关于为什么我不能在 switch 语句中使用元组常量作为案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我不能在 switch 语句中使用元组常量作为案例
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01