Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJOSUE YU
2,058 PointsWhat does the keyword "as" means?
Hello, I see a lot of "as", what does it means? Is it like defining a type?
For example : let playlistImageView = sender!.view as UIImageView
Thank you in advance
2 Answers
kjvswift93
13,515 PointsWhen you assign the result of a previously declared variable to one that is of a different type, you use "as" to format it as the same type in which the variable was first declared. For example, in the Playlist struct, we created a variable called title that was of type String Optional. (var title: String?) When the variable "title" is then assigned to the key "title" in a dictionary, you must say "as String!" because a string type cannot be assigned to a dictionary type. By saying "as String!", you are assigning a string type to another string type instead of a string to a dictionary key.
Alex Hedley
16,381 PointsIt's a way of saying something is of a certain Type.
In the Swift Docs it is also described as Downcasting
Downcasting A constant or variable of a certain class type may actually refer to an instance of a subclass behind the scenes. Where you believe this is the case, you can try to downcast to the subclass type with the type cast operator (as).
Martin Shay
5,919 Pointshere is a more direct link to apple's documentation on downcasting.
Noel Deles
8,215 PointsNoel Deles
8,215 PointsJust for clarification, in this case then, sender!.view returns a value of what type then? Am I right in stating that all UIImageView's are views, but not all view's are UIImageViews? Thanks for the help.