Swift is based off of Cocoa Touch.
The swift programming language is based off of Cocoa Touch, which has the same runtime as Objective-C. Apple wanted to make them similar, so you can run both Objective-C, and Swift in the same project.
Basic Swift Syntax
This line creates a variable, and sets it equal to the number 9. Swift does not allow you to make a variable, and set it equal to nothing.
var myVar = 9
This line is very similar to the first line. The syntax "let," is used as a constant. Typically a variable would be used if it is a number. Constants can not be changed, if you needed that, you would have to use a variable.
let myConstant = 7
You can explicitly cast a variable or constant to an object type.
let myConstant: Int = 7
let myVar: Int = 8
Simple data types:
Int: This holds a regular number, but it can NOT hold decimal.
Float: This holds a regular number, but it CAN hold a decimal.
Char: This holds a simple character. For example, I can set it to the character A or B.
This line is used to print a line. This is often used for debugging an iOS app, which we will not get to, and command line tools.
println("Hello, world!")
I can also have a println statement print a constant or variable.
var myFirstVar: Float = 379.344
println("\(myFirstVar)")
You can also use String's, which hold text. I can make a let with a string, and then a let with a var, and combine them.
var text = "The number of bottles of soda sold this month is "
var numberOfSodas = 89
var wholeSentence = text + String(numberOfSodas)
You can make variables, and add them to a variable with a String.
var amountOfCoke = 34
var amountOfSprite = 39
var cokeSummary = "There are \(amountOfCoke) bottles of Coke."
var spriteSummary = "There are \(amountOfSprite) bottles of Sprite."
Instead of using an NSArray in Objective-C, you can simply use a variable.
var myArray = [1, 2, 3, 4]
var mySecondArray = ["Jeff", "Henry"]
You can make a simple for, or for-in statement to make loops.
let theScores = [64, 65, 86, 99, 100]
var score = 0
for score in theScores {
if score > 50 {
theScores += 3
} else {
theScores += 1
}
}
You can create an optional variable also.
var optionalString: String? = "Hello"
optionalString = nil
You can use a switch also.
let theFood = "ice cream"
switch theFood {
case "sandwhich":
let theFoodComment = "It's a sandwhich."
case "ice cream":
let theFoodComment = "It's ice cream!"
case "pizza":
let theFoodComment = "It's pizza!"
default:
let theFoodComment = "There are tons of good foods!"
}
There is also a function, which is like a method in Objective-C. They are very useful. After the "->," it returns a value. Inside the parentheses are the parameters.
func myFunction(name: String, month: String, day: Int) -> String {
return "My name is \(name), and it is the \(Day) of \(month)"
}
myFunction("John", "June", 23)
var myVar = 9
This line is very similar to the first line. The syntax "let," is used as a constant. Typically a variable would be used if it is a number. Constants can not be changed, if you needed that, you would have to use a variable.
let myConstant = 7
You can explicitly cast a variable or constant to an object type.
let myConstant: Int = 7
let myVar: Int = 8
Simple data types:
Int: This holds a regular number, but it can NOT hold decimal.
Float: This holds a regular number, but it CAN hold a decimal.
Char: This holds a simple character. For example, I can set it to the character A or B.
This line is used to print a line. This is often used for debugging an iOS app, which we will not get to, and command line tools.
println("Hello, world!")
I can also have a println statement print a constant or variable.
var myFirstVar: Float = 379.344
println("\(myFirstVar)")
You can also use String's, which hold text. I can make a let with a string, and then a let with a var, and combine them.
var text = "The number of bottles of soda sold this month is "
var numberOfSodas = 89
var wholeSentence = text + String(numberOfSodas)
You can make variables, and add them to a variable with a String.
var amountOfCoke = 34
var amountOfSprite = 39
var cokeSummary = "There are \(amountOfCoke) bottles of Coke."
var spriteSummary = "There are \(amountOfSprite) bottles of Sprite."
Instead of using an NSArray in Objective-C, you can simply use a variable.
var myArray = [1, 2, 3, 4]
var mySecondArray = ["Jeff", "Henry"]
You can make a simple for, or for-in statement to make loops.
let theScores = [64, 65, 86, 99, 100]
var score = 0
for score in theScores {
if score > 50 {
theScores += 3
} else {
theScores += 1
}
}
You can create an optional variable also.
var optionalString: String? = "Hello"
optionalString = nil
You can use a switch also.
let theFood = "ice cream"
switch theFood {
case "sandwhich":
let theFoodComment = "It's a sandwhich."
case "ice cream":
let theFoodComment = "It's ice cream!"
case "pizza":
let theFoodComment = "It's pizza!"
default:
let theFoodComment = "There are tons of good foods!"
}
There is also a function, which is like a method in Objective-C. They are very useful. After the "->," it returns a value. Inside the parentheses are the parameters.
func myFunction(name: String, month: String, day: Int) -> String {
return "My name is \(name), and it is the \(Day) of \(month)"
}
myFunction("John", "June", 23)