01-Assignment and arithmetic operators
// assignment operator
let (regular task, overtime task) = (4, 2)
// arithmetic operator
1 + 1
7-3
3 * 4
10.0 / 6
02-Excess and increase and decrease
// remainder operator
10% 6
10% -6
-10% 6
// take the remainder of the floating-point number
1024% 3.5
// Increment and decrement
var Like = 0
// Increment first and then return, this method is recommended
++ Like
// return first and then increment
Like ++
// string concatenation
"Puppy" + "??"
03- negative and combination assignment
// take negative
let three = 3
let minus three = -three
// combined assignment statement
var number = 1
// equivalent to number = number + 3
number + = 3
04-Comparison and ternary operators
// comparison operator
1 == 1
2! = 1
2> 1
1 <2
1> = 1
2 <= 1
let welcome message = "Welcome to learn Swift"
if (welcome message == "Welcome to learn Swift") {
println ("You are very welcome")
} else {
println ("Ha ha")
}
// Ternary operator
let wholesale price = 50
let member = false
let basic markup = 20
let member markup = 10
var price = wholesale price + (member? member markup: basic markup)
// Use if else to implement the above function
if (member) {
Sale price = wholesale price + basic markup
} else {
Sale price = wholesale price + member's markup
}
5-closed interval and half interval
// closed interval
for year in (1949 ... 2015) {
println ("\ (year) is the \ (year-1949 + 1) year")
}
// open range
var star list = ["Andy Lau", "Li Jetie", "Guo Fucheng", "Zhang Xueyou"]
var number of stars = list of stars. count
for sort order in 0 .. <number of stars {
println ("The \ (ranking order + 1) star is \ (star list [order]]")
}
6-logical operators
// Logical Operators
let enter = true
// logical NOT
if! please enter {
println ("can enter")
} else {
println ("Deny enter")
}
// logical AND and logical OR
let password is correct = false
let retina correct = false
let master key = true
if (password correct && retina correct || master key) {
println ("can enter")
} else {
println ("Deny enter")
}
07-Selection operator
// select and operator
let bottom line brand = "Xiaomi"
// optional variables
var Ideal Brand: String?
Ideal Brand = "iPhone 6"
var Actual Start = (Ideal Brand ?? Bottom Line Brand)
Swift basic operators