First, the basic operator
let a = 5
var b = ten
b = A
if a = b{
The assignment operator in Swift does not return itself as a value, so compiling is illegal, helping developers avoid mistakes, very human language
}
Second, mathematical operators
let dog:Character = "?? "
let cow:Character = "?? "
let dogcow = dog + Cow
println(dogcow)
//Swift arithmetic operator does not allow overflow, by overflow operator to select the value overflow condition (a& + b)
The // remainder operator can be used for floating-point numbers flost
8 % 2.5 //equals 0.5
//unary minus operator
let three = 3
let minustree =-three
let plusthree =-minustree
println(minustree)
println(plusthree)
//unary plus operator
Three, scope operators
= = =! = = Two identity operator detects whether two object references are from the same object instance
Two ranges
Enclosing (a...b) means: A to B value
For index in 1...5{
println ()
}
Semi-closed
(a). b), Baotou does not pack tail
let names = ["Anna","Alex","BBSC","SDFs"]
Let count = names. Count
for i in 0... count-1{
println("Person \ (i) is called \ (Names[i])")
}
Four, characters and strings
Shortcut keys:
Option +y = "Y"
Emoji = control + commod +space
The string in Swift is not the actual value of the pointer
Initializing an empty string
var emptystring = ""
var anotheremptystr = String ()
If emptystring.isempty{
}
var variablestring = "Horse"
Variablestring + = "and carriage"
Countelements calculating the number of characters in a string
Let countstr = "Hahahh"
println (countstr count is \ (countelements (COUNTSTR)))
The length of the NSString is based on the number of UTF-16 encodings and not on Unicode
The string in swift does not necessarily occupy the same memory space.
string comparison
Let SOMESTR1 = "abc"
Let SOMESTR2 = "abc"
if someStr1 = = somestr2{
}
The string in Swift is not the actual value of the pointer
Prefix equal
Let animals = ["Meat: Tiger", "Carnivorous: Lion", "Grass: Sheep", "Grass: Cattle", "Grass: Horse")
var acount = 0
For animal in animals{
If animal.hasprefic ("meat") {
++acount
}
}
println ("Here are \ (acount) head carnivores")
Converting string case
Let normal = "could to help me"
Let Shoty = normal.uppercasestring//Uppercase
Let whispered = normal.lowercasestring//lowercase
The string encoding output is printed using the C language
. UTF8. utf16
Unicode scalar
. unicodescalars
Put a space on the output
For scalar in Dogstring.unicodescalars
{
Print ("\ (scalar)")
}
Swift Basics-2