Changes

Swift Data Types, Constants and Variables

3,666 bytes added, 21:26, 13 April 2015
Summary
var myString: String = nil // Invalid Code
let myConstant = nil // Invalid code
</pre>
 
== Type Casting and Type Checking ==
 
When writing Swift code, situations will occur where the compiler is unable to identify the specific type of a value. This is often the case when a value of ambiguous or unexpected type is returned from a method or function call. In this situation it may be necessary to let the compiler know the type of value that your code is expecting or requires using the as keyword (a concept referred to as type casting).
 
The following code, for example, lets the compiler know that the value returned from the objectForKey method needs to be treated as a String type:
 
<pre>
let myValue = record.objectForKey("comment") as! String
</pre>
 
In fact, there are two types of casting which are referred to as upcasting and downcasting. Upcasting occurs when an object of a particular class is cast to one of its superclasses. Upcasting is performed using the as keyword and is also referred to as guaranteed conversion since the compiler can tell from the code that the cast will be successful. The UIButton class, for example, is a subclass of the UIControl class as shown in the fragment of the UIKit class hierarchy shown in Figure 7-1:
 
 
[[Image:uikit_tree_button.png|Swift upcasting diagram]]
 
Figure 7-1
 
 
Since UIButton is a subclass of UIControl, the object can be safely upcast as follows:
 
<pre>
let myButton: UIButton = UIButton()
 
let myControl = myButton as UIControl
</pre>
 
Downcasting, on the other hand, occurs when a conversion is made from one class to another where there is no guarantee that the cast can be made safely or that an invalid casting attempt will be caught by the compiler. When an invalid cast is made in downcasting and not identified by the compiler it will most likely lead an error at runtime.
 
Downcasting usually involves converting from a class to one of its subclasses. Downcasting is performed using the as! keyword syntax and is also referred to as forced conversion. Consider, for example, the UIKit UIScrollView class which has as subclasses both the UITableView and UITextView classes as shown in Figure 7-2:
 
[[Image:uikit_tree.png|Swift downcast diagram]]
 
Figure 7-2
 
 
In order to convert a UIScrollView object to a UITextView class a downcast operation needs to be performed. The following code attempts to downcast a UIScrollView object to UITableView using the guaranteed conversion or upcast approach:
 
<pre>
let myScrollView: UIScrollView = UIScrollView()
 
let myTextView = myScrollView as UITextView
</pre>
 
When compiled, the above code will result in an the following error:
 
<pre>
‘UIScrollView’ is not convertible to ‘UITextView’
</pre>
 
The compiler is indicating that a UIScrollView instance cannot be safely converted to a UITextView class instance. This does not mean that it is incorrect to do so, the compiler is simply stating that it cannot guarantee the safety of the conversion for you. The downcast conversion must instead be forced using the as! Annotation:
 
<pre>
let myTextView = myScrollView as! UITextView
</pre>
 
Another useful approach to downcasting is to perform an optional binding using as?. If the conversion is performed successfully, an optional value of the specified type is returned, otherwise the optional value will be nil:
 
<pre>
if let classB = classA as? UITextView {
println("Type cast to UITextView succeeded")
} else {
println("Type cast to UITextView failed”)
}
</pre>
 
It is also possible to type check a value using the is keyword. The following code, for example, checks that a specific object is an instance of a class named MyClass:
 
<pre>
if myobject is MyClass {
// myobject is an instance of MyClass
}
</pre>