swift 对象转换_Swift类型转换–照原样,任何对象

swift 对象转换

In this tutorial, we’ll be looking into the details of Swift Type Casting. Let’s get started on our Xcode Playground!

在本教程中,我们将研究Swift Type Casting的细节。 让我们开始在我们的Xcode Playground!

什么是Swift类型转换? (What is Swift Type Casting?)

Broadly, Type Casting consists of two things:

大致来说,类型转换包含两件事:

  • Type Checking

    类型检查
  • Changing the Type

    改变类型
  • is operator is used to check the type of an instance.

    is运算符,用于检查实例的类型。
  • as operator is used to cast the instance to another type.

    as运算符用于将实例转换为其他类型。

快速类型检查 (Swift Type Checking)

Swift gives a lot of priority to the readability of the code. No wonder to check whether an instance belongs to a certain type, we use the is keyword.

Swift对代码的可读性给予了很多优先考虑。 难怪要检查实例是否属于某种类型,我们使用is关键字。

For Example, the following code snippet would always print false.

例如,以下代码片段将始终显示false。

var isString = Int.self is String 
print(isString) // false

Let’s do the type checking in the class and its subclasses. For that, we’ve created the following three classes.

让我们在及其子类中进行类型检查。 为此,我们创建了以下三个类。

class University
{
    var university: String
    init(university
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值