swift 类型转换

  1. var movieCount = 0
  2. var songCount = 0
  3. for item in library {
  4. if item is Movie {
  5. movieCount += 1
  6. } else if item is Song {
  7. songCount += 1
  8. }
  9. }
  1. for item in library {
  2. if let movie = item as? Movie {
  3. print("Movie: \(movie.name), dir. \(movie.director)")
  4. } else if let song = item as? Song {
  5. print("Song: \(song.name), by \(song.artist)")
  6. }
  7. }

Type Casting for Any and AnyObject

Swift provides two special types for working with nonspecific types:

  • Any can represent an instance of any type at all, including function types.

  • AnyObject can represent an instance of any class type.

To discover the specific type of a constant or variable that is known only to be of type Any or AnyObject, you can use an is or as pattern in a switch statement’s cases. The example below iterates over the items in the things array and queries the type of each item with a switch statement. Several of the switch statement’s cases bind their matched value to a constant of the specified type to enable its value to be printed:

  1. for thing in things {
  2. switch thing {
  3. case 0 as Int:
  4. print("zero as an Int")
  5. case 0 as Double:
  6. print("zero as a Double")
  7. case let someInt as Int:
  8. print("an integer value of \(someInt)")
  9. case let someDouble as Double where someDouble > 0:
  10. print("a positive double value of \(someDouble)")
  11. case is Double:
  12. print("some other double value that I don't want to print")
  13. case let someString as String:
  14. print("a string value of \"\(someString)\"")
  15. case let (x, y) as (Double, Double):
  16. print("an (x, y) point at \(x), \(y)")
  17. case let movie as Movie:
  18. print("a movie called \(movie.name), dir. \(movie.director)")
  19. case let stringConverter as (String) -> String:
  20. print(stringConverter("Michael"))
  21. default:
  22. print("something else")
  23. }
  24. }
  25. // zero as an Int
  26. // zero as a Double
  27. // an integer value of 42
  28. // a positive double value of 3.14159
  29. // a string value of "hello"
  30. // an (x, y) point at 3.0, 5.0
  31. // a movie called Ghostbusters, dir. Ivan Reitman
  32. // Hello, Michael
 

转载于:https://www.cnblogs.com/feng9exe/p/9105980.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Swift 中,可以使用 `is` 关键字来进行类型判断。 示例: ``` let x = 5 if x is Int { print("x is an integer") } ``` 另外,Swift 还提供了 `type(of:)` 函数来获取一个值的类型。 示例: ``` let x = 5 let xType = type(of: x) print(xType) // 输出 "Int" ``` ### 回答2: 在Swift中,类型判断函数是一种用来判断一个对象是否属于特定类型的函数。在Swift中,我们可以使用`is`关键字来进行类型判断。 具体来说,我们可以使用类型判断函数来检查一个对象是否属于某个具体的类型,或者是它的子类。这可以帮助我们在运行时根据对象的实际类型来进行不同的操作。 下面是一个简单的示例,展示了如何使用类型判断函数: ```swift class Animal { var name: String init(name: String) { self.name = name } } class Dog: Animal { func bark() { print("Woof!") } } class Cat: Animal { func meow() { print("Meow!") } } let animals: [Animal] = [Dog(name: "Buddy"), Cat(name: "Whiskers"), Dog(name: "Max")] for animal in animals { if animal is Dog { let dog = animal as! Dog dog.bark() } else if animal is Cat { let cat = animal as! Cat cat.meow() } } ``` 在上面的示例中,我们创建了一个包含不同类型动物的数组。通过使用`is`关键字,我们可以判断每个元素是否是`Dog`类型或`Cat`类型。如果是`Dog`类型,我们将对象强制转换为`Dog`类,并调用`bark()`方法;如果是`Cat`类型,我们将对象强制转换为`Cat`类,并调用`meow()`方法。 类型判断函数是Swift中非常强大和灵活的特性之一,它使得我们可以根据对象的类型执行不同的操作,从而提高代码的可读性和灵活性。 ### 回答3: 在Swift中,类型判断函数是一种用于判断给定实例的类型的功能。它可以帮助开发者在运行时确定某个对象的具体类型,从而有针对性地执行相应的操作。在使用类型判断函数时,我们可以使用关键字"is"或"as"来判断对象的类型。 使用"is"关键字可以判断某个对象是否属于特定类型,返回一个布尔值。例如: ``` if someVariable is String { print("someVariable是String类型") } ``` 在上述代码中,我们使用"is"关键字判断`someVariable`是否为`String`类型。 除了使用"is"关键字之外,我们还可以使用"as"关键字进行类型转换。"as"关键字有两种用法,分别为向下转型和强制类型转换。 向下转型(Downcasting)是将一个父类类型转换为其子类类型。如果转换成功,将返回子类类型的可选值,否则返回nil。例如: ``` class Animal {} class Dog: Animal {} let animal: Animal = Dog() if let dog = animal as? Dog { print("向下转型成功") } ``` 在上述代码中,我们将一个Animal类型的实例向下转型为Dog类型,并判断转型是否成功。 强制类型转换(Forced Type Casting)是将一个类型强制转换为另一个类型,前提是两个类型存在继承关系。如果转换成功,将返回转换后的非可选值,否则会引发运行时错误。例如: ``` class Animal {} class Dog: Animal {} let animal: Animal = Dog() let dog = animal as! Dog print(dog) ``` 在上述代码中,我们将一个Animal类型的实例强制转换为Dog类型,并打印转换成功后的结果。 总而言之,Swift中的类型判断函数能够帮助我们在运行时确定对象的类型,以便执行相应的操作。具体可以通过判断关键字"is"或"as"来实现类型判断和类型转换

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值