ielyamani
ielyamani

Reputation: 18591

As operator in Swift

What does the as operator expect as a right hand side argument?

and why wouldn't it accept a ClassName.self or variableName.dynamicType?

My question comes down to the difference between .self, .dynamicType and a real class name?

Upvotes: 1

Views: 97

Answers (1)

Fujia
Fujia

Reputation: 1242

as operator expect a static Type as the right hand argument. Here the word static means something known to the compiler at compile time.

Both .self and .dynamicType are expressions, and therefore are not expected by as operator. Expressions are evaluated at runtime. In the case of .self and .dynamicType, they return a Type after evaluation.

Anyway, as operator does not expect something to be evaluated to be its right hand argument, but a "hard-coded" Type. This feature allows Swift to enforce compile time checking.

Hope this makes sense.

Upvotes: 4

Related Questions