Earlier before swift 2.0 we were using "as" keyword for type casting but now it told me to convert it into "as!" At some places why?
1 Answers
The change was made in Swift 1.2
From the Swift Blog
The as! Operator
Prior to Swift 1.2, the
as
operator could be used to carry out two different kinds of conversion, depending on the type of expression being converted and the type it was being converted to:Guaranteed conversion of a value of one type to another, whose success can be verified by the Swift compiler. For example, upcasting (i.e., converting from a class to one of its superclasses) or specifying the type of a literal expression, (e.g.,
1 as Float
). Forced conversion of one value to another, whose safety cannot be guaranteed by the Swift compiler and which may cause a runtime trap. For example downcasting, converting from a class to one of its subclasses. Swift 1.2 separates the notions of guaranteed conversion and forced conversion into two distinct operators. Guaranteed conversion is still performed with theas
operator, but forced conversion now uses theas!
operator. The!
is meant to indicate that the conversion may fail. This way, you know at a glance which conversions may cause the program to crash.Note the analogy between the expression postfix operators
!
and?
and the conversion operatorsas!
andas?

- 274,689
- 30
- 353
- 361