Swift中Switch中不同数据类型的匹配判断
程序员文章站
2022-05-07 09:13:04
...
实例代码如下
这里面会根据thing的值进行匹配,到对应的case当中
var things = Any[]() things.append(0) things.append(0.0) things.append(42) things.append(3.14159) things.append("hello") things.append((3.0, 5.0)) things.append(Movie(name: "Ghostbusters", director: "Ivan Reitman")) for thing in things { switch thing { case 0 as Int: println("zero as an Int") case 0 as Double: println("zero as a Double") case let someInt as Int: println("an integer value of \(someInt)") case let someDouble as Double where someDouble > 0: println("a positive double value of \(someDouble)") case is Double: println("some other double value that I don't want to print") case let someString as String: println("a string value of \"\(someString)\"") case let (x, y) as (Double, Double): println("an (x, y) point at \(x), \(y)") case let movie as Movie: println("a movie called '\(movie.name)', dir. \(movie.director)") default: println("something else") } } // zero as an Int // zero as a Double // an integer value of 42 // a positive double value of 3.14159 // a string value of "hello" // an (x, y) point at 3.0, 5.0 // a movie called 'Ghostbusters', dir. Ivan Reitman
这里面会根据thing的值进行匹配,到对应的case当中
下一篇: gitlab安装-总结
推荐阅读
-
数据类型 PHP中=赋值操作符对不同数据类型的不同行为
-
mysql-请问:用java代码把不同的excel形式加载到MySQL数据库中(循环判断空就停止)?谢谢!
-
mysql-请问:用java代码把不同的excel形式加载到MySQL数据库中(循环判断空就停止)?谢谢!
-
PHP中=赋值操作符对不同数据类型的不同行为
-
javascript中如何判断对象的数据类型?
-
PHP中=赋值操作符对不同数据类型的不同行为
-
Java switch() case中的switch可用的数据类型
-
JavaScript普通函数中的this指向判断,箭头函数的this有什么不同?
-
JavaScript中数据类型的判断——typeof,instanceof,constructor,Object.prototype.toString.call()
-
JS中的数据类型的判断(typeof,instanceof,constructor)