"as" in Swift
发布时间:2020-12-14 06:20:17 所属栏目:百科 来源:网络整理
导读:逐条解释一下,熟悉 “as”的用法,是“as”,不是“as?”和 “as!”: for thing in things { switch thing { case 0 as Int : print ( "zero as an Int" ) case 0 as Double : print ( "zero as a Double" ) case let someInt as Int : print ( "an integ
逐条解释一下,熟悉 “as”的用法,是“as”,不是“as?”和 “as!”: for thing in things {
switch thing {
case 0 as Int:
print("zero as an Int")
case 0 as Double:
print("zero as a Double")
case let someInt as Int:
print("an integer value of (someInt)")
case let someDouble as Double where someDouble > 0:
print("a positive double value of (someDouble)")
case is Double:
print("some other double value that I donot want to print")
case let someString as String:
print("a string value of "(someString)"")
case let (x,y) as (Double,Double):
print("an (x,y) point at (x),(y)")
case let movie as Movie:
print("a movie called (movie.name),dir.(movie.director)")
case let stringConverter as (String) -> String:
print(stringConverter("Michael"))
default:
print("something else")
}
}
(1) 0 as Int
(2) 0 as Double
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |