Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Downcast to known type

Tags:

swift

optional

I was just wondering…

let movie = item as? Movie

In this example item is either of type Movie or something else. In case of the latter movie will be assigned a nil value. Thus movie will be of type Movie?.

Now check out the next example:

let movie: Movie?

movie = item as? Movie

Let's say movie is a class variable which I'm assigning some value inside one of my class methods.

To me it feels like as? Movie is somewhat redundant, since movie is already known to be of type Movie?.

Is there any kind of syntax in Swift that says: "Downcast this object to the same type as the object to which I'm assigning it to. Assign nil if this is an optional type and the downcast fails."?

I was hoping for something like:

movie = item as?

or

movie =? movie
like image 613
Pim Avatar asked Jul 14 '15 07:07

Pim


People also ask

How do I cast type in Swift?

Type casting in Swift is implemented with the is and as operators. These two operators provide a simple and expressive way to check the type of a value or cast a value to a different type. You can also use type casting to check whether a type conforms to a protocol, as described in Checking for Protocol Conformance.

What is the difference between Upcast and downcast in Swift?

Downcasting is the opposite of upcasting, and it refers to casting an object of a parent class type to an object of its children class. Downcasting is used to reconvert objects of a children class that were upcasted earlier to generalize. Let's say you own two cars and three trucks.

Did you mean to use as !' To force downcast?

( as! ), which is know to be the Forced Form, attempts the downcast and force-unwraps the result as a single compound action. You should use it ONLY when you are sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type.

What is type casted?

typecast in British English (ˈtaɪpˌkɑːst ) verbWord forms: -casts, -casting or -cast. (transitive) to cast (an actor) in the same kind of role continually, esp because of his or her physical appearance or previous success in such roles.


2 Answers

The existing answers correctly state that there is no built in functionality for this. But just to build on what's already been written here, it is possible to define an infix operator =? which implements the behavior you described in your question. For example:

infix operator =? {
    associativity none
    precedence 130
}

func =? <T>(inout lhs: T?, rhs: Any?) {
    lhs = rhs as? T
}

var movie: Movie?
let item: Any? = Movie(name: "some movie")

movie =? item
movie?.name // "some movie"
like image 184
Mick MacCallum Avatar answered Sep 23 '22 14:09

Mick MacCallum


This is not possible.

See the type casting section of the swift documentation below:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_514

like image 34
Arno van Lieshout Avatar answered Sep 24 '22 14:09

Arno van Lieshout