Swift Any、Type、is、as

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift Any、Type、is、as相关的知识,希望对你有一定的参考价值。

参考技术A is , as , as? , as!
is: 判断是否为某种类型
as: 用来做强制转换

There is no type initializer in Swift----One answer is to use static, it is the same as class final.

“Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks


You could use a type property which default value is a closure. So the code in the closure would be executed when the type property (or class variable) is set.

But class stored properties not yet supported (tested in Xcode 8).

One answer is to use static, it is the same as class final.

Good link for that is

Setting a Default Property Value with a Closure or Function

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks


Code example:

Prints

start

setting default property value with a closure

So it is lazy evaluated.

 

https://stackoverflow.com/questions/24137212/initialize-class-method-for-classes-in-swift

以上是关于Swift Any、Type、is、as的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 Type 'Any' 没有下标成员

swift 3 Type 'Any' 没有下标成员? [复制]

类型'Any'没有下标成员Swift 3

swift 3.0Type 'Any?' has no subscript members

Exception in thread “main“ java.lang.UnsupportedOperationException: Schema for type Any is not suppo

Swift基础-AnyObject&Any&AnyClass