Revision: 68779
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 23, 2015 06:42 by chrisaiv
Initial Code
////////////////////////////////
//Strings + Error Protection
////////////////////////////////
import UIKit
/** ** ** ** ** ** ** ** ** ** **
Types of Strings
** ** ** ** ** ** ** ** ** ** **/
//Simple String or Non-Optional String
var name:String
name = "Chris"
//Optional String
var jobFunction:String?
jobFunction = "Developer"
//Implicitely unwrapped Optional String
var jobDept:String!
jobDept = "Dev Team"
/** ** ** ** ** ** **
Optional String? Use Cases
** ** ** ** ** ** **/
//A. Print Optional String
if(jobFunction != nil){
//Without Force Unwrapping
println("Access my inner \(jobFunction)")
//With Force Unwraping
println("Access my inner \(jobFunction!) through unwrapping!")
} else {
println("jobFunction is nil")
}
//B. Optional-binding Syntax: if true, then assign the value to a CONSTANT
if let jobTitle = jobFunction {
//Since jobFunction exists, the name is assigned to jobTitle
println(jobTitle)
}
/** ** ** ** ** ** **
Implicit String! Use Cases
** ** ** ** ** ** **/
//A. Print Implicite String
if jobDept != nil {
println("My department is \(jobDept)")
} else {
println("jobDept is nil")
}
//How to protect yourself from force unwrapping a nil value
//B. Nil-coalescing operator: Check variable to see if it's nil. If it's not nil, use that value, otherwise, use something else
let finalName = (name ?? "no name")
let finalJob = (jobFunction ?? "no job")
Initial URL
Initial Description
I've been learning swift lately and here are a few basics.
Initial Title
Swift: String basics
Initial Tags
Initial Language
Objective C