Changes

Swift Data Types, Constants and Variables

625 bytes added, 17:48, 13 April 2015
Type Annotations and Type Inference
During compilation of the above lines of code, Swift will infer that the signalStrength variable is of type Double (type inference in Swift defaults to Double for all floating point numbers) and that the companyName constant is of type String.
 
When a constant is declared without a type annotation it must be assigned a value at the point of declaration:
 
<pre>
let bookTitle = "iOS 8 Development Essentials"
</pre>
 
If a type annotation is used when the constant is declared, however, the value can be assigned later in the code. For example:
 
<pre>
let bookTitle: String
.
.
if iosBookType {
bookTitle = "iOS 8 Development Essentials"
} else {
bookTitle = "Android Studio Development Essentials"
}
</pre>
 
It is important to note that a value may only be assigned to a constant once. A second attempt to assign a value to a constant will result in a syntax error.
== Type Casting and Type Checking ==