美文网首页
swift3.1 类型转换

swift3.1 类型转换

作者: 岁月蹉跎繁华落寞 | 来源:发表于2017-04-27 23:46 被阅读0次
  • is
var movieCount = 0
var songCount = 0
 
for item in library {
    if item is Movie {
        movieCount += 1
    } else if item is Song {
        songCount += 1
    }
}
 
print("Media library contains \(movieCount) movies and \(songCount) songs")
// Prints "Media library contains 2 movies and 3 songs"
  • as!

  • as?

for item in library {
    if let movie = item as? Movie {
        print("Movie: \(movie.name), dir. \(movie.director)")
    } else if let song = item as? Song {
        print("Song: \(song.name), by \(song.artist)")
    }
}
  • Any

不仅仅能够容括class类型 同时还可以适用于包括struct 和enum在内的所有类型

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"))
things.append({ (name: String) -> String in "Hello, \(name)" }) 
  • AnyObject

本身就是一个接口

protocol AnyObject { }

而且所有的class都隐式的实现了这个接口,这也限制了AnyObject是只适用于Class类型的原因。

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 don't 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")
    }
}
 
// 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
// Hello, Michael

NOTE

The Any type represents values of any type, including optional types. Swift gives you a warning if you use an optional value where a value of type Any is expected. If you really do need to use an optional value as an Any value, you can use the as operator to explicitly cast the optional to Any, as shown below.

let optionalNumber: Int? = 3
things.append(optionalNumber)        // Warning
things.append(optionalNumber as Any) // No warning

相关文章

  • swift3.1 类型转换

    is as! as? Any 不仅仅能够容括class类型 同时还可以适用于包括struct 和enum在内的所有...

  • C语言类型转换

    类型转换的方式 自动类型转换1.运算转换(规则:会把小类型转换为大类型) 2.赋值转换 强制类型转换格式:(类型)...

  • 引用类型转换

    向上类型转换(自动类型转换),是小类型到大类型的转换向下类型转换(强制类型装换),是大类型到小类型的转换在强制类型...

  • 第三章 JavaScript中的数据间类型的转换

    数据的类型转换分为强制类型转换和非强制类型转换,强制类型转换也可以称为显示转换,非强制类型转换也可以称为隐式转换。...

  • 笔记2018-08-28

    类型转换 自动类型转换运算转换(编译器会自动把小类型转换为大类型) 赋值转换(将等号右边的类型转换为等号左边的类型...

  • 四、SQL函数④(其他函数)

    类型转换 隐式转换:数据库系统自动将字符串类型转换为整数类型显式转换:使用类型转换函数转换。使用类型转换函数不仅可...

  • 4_类型转换

    关键词:类型之间的转换、强制类型转换、隐式类型转换 1. 类型之间的转换 C语言中的数据类型可以进行转换,转换方式...

  • 学习记录

    类型转换as?类型转换,转换成功,返回一个可选类型,转换不成功返回nilas!类型转换,转换失败回报运行错误备注:...

  • 回顾Date.0402(类型转换、运算)

    类型转换 隐式转换 显式转换 转换为Boolean类型:Boolean(xxx) ; !!xxx 数字类型转换为布...

  • JavaScript的强制类型转换

    强制类型转换 将javascript的值从一种类型转换为另一种类型的值——>类型转换隐式类型转换——>强制类型转换...

网友评论

      本文标题:swift3.1 类型转换

      本文链接:https://www.haomeiwen.com/subject/uaznzttx.html