Building apps, and programming in general, is all about working with data. As a developer, you'll need to understand how to handle and store data using clearly defined types.
When you want to name a vtalue that won't change during the lifetime of the program, you'll use a constant. You define constants in Swift using the let keyword.
let newstr = "Hello, I'm a constant"
Note: You can't give a constant a new value once it has been assigned.
When you want to name a value that may change during the lifetime of the app, you'll use a variable. You define variables using the var keyword.
var str = "Hello, I'm a variable"
Type Inference Swift makes assumptions about the type based on the value assigned.
var infered = "This var is assumed as type String"
Type Annotation Explicitly specify the type of the constant or variable. To specify a type, add a colon (:), a space, and the type name following the constant or variable name.
var integer : Int = 1
IMPORTANT : Constant and Variable names can't contain – Mathematical Symbols, Spaces, and when it begins with a number.
Specify a block of code to execute if the condition is not true.
//Set boolean variables
var choice = true
var choice2 = false
if choice == true || choice2 == true{
print("Test succeeded")
} else if choice == true && choice2 == false{
print("Yehey")
} else {
print("Else goes here")
}
Note: In Swift, there's no need to enclose conditions in parenthesis().
A basic switch statement takes a value with multiple options and allows you to run separate code based on each option, or case. You can also provide a default case to specify a block of code that will run in all the cases you haven't specifically defined.
let switchtest = "A"
switch switchtest {
case "A":
print("Switch case catch letter \(switchtest)")
case "B","C":
print("Switch case catch letter \(switchtest)")
fallthrough
default:
print("\(switchtest) not in Switch Case")
}
Multiple Conditions Evaluate multiple conditions by simply separating with commas
Fallthrough Falls through to the next case, not to the next matching case
A for loop is useful for repeating something a set number of times or for performing work across a collection of values. It executes a set of statements for each item within a range, sequence, or collection.
for index in 1...5 {
print("This is number \(index)")
}
1...5 range of the loop. The value of index starts with the first value in range
//Creates an array of Strings
var names : [String] = ["Anna","Bert","Calvin","Dexter","Einstein"]
print(names)
for (index, name) in names.enumerated(){
print("\(name) is at index \(index)")
}
enumerated() returns a tuple containing both the index and the value of each item
A while loop will continue to loop until its specified condition is no longer true.
var names : [String] = ["Anna","Bert","Calvin","Dexter","Einstein"]
var countWhile = 0
while (countWhile < names.count){
print("While result: \(names[countWhile])")
countWhile = countWhile + 1
}
Important: Avoid infinite loop by making sure the condition will be satifisfied at some point. In the above example, we increment our countWhile variable which serves as our loop counter.
Unlike While - on Repeat While the statement will be executed first before the condition is evaluated.
var test = false
repeat{
print("Hello!!")
test = true
} while (test != true)
Stores an ordered list of same-typed values. When you declare an array, you can specify what type of values will be held in the collection, or you can let the type inference system discover the type
var names : [String] = ["Anna","Bert","Calvin","Dexter","Einstein"]
An array is like a basket: It can start out empty and you can fill it with values at a later time.
var myArray: [Int] = []
It is a list of keys, each with an associated value. Each key must be unique, just like each word in the dictionary is unique.
var professions : [String : (String, Int)] = ["Joey" : ("Doctor",8) , "Andrea" : ("Nurse",10), "Eugene" : ("Engineer",9)]
print(professions)
print(professions["Joey"]!)
[key1 : value1, key2: value2, key3: value3]
Empty Dictionaries
var myDictionaryOne = Dictionary<String, Int>()
var myDictionaryTwo : [String: Int] = [:]”
A special type that can hold an ordered list of values wrapped in parentheses– that holds the key and value of each entry
var person = (name: "Ciela",age: 20)
print(person.name)
Similar with Array but it stores an UN-ORDERED list of same-typed values
var sampleSet : Set<Int> = [1,2,3,4,5]
print(sampleSet)
for (index, value) in sampleSet.enumerated(){
print("\(index) : \(value)")
}
A function is the idea of taking something that is complex and defining a simpler way to refer to it is an abstraction.
func printName(name: String) -> (String, Int){
return ("Hello \(name)", 20)
}
printName(name: "Ciela")
A function is made up of three things: its name, an optional list of parameters, and an optional return type.
App Development with Swift Apple Inc. - Education, 2017.