1 //常量 let pi=3.33 //变量 var ene=100 var length = 10, width = 20, height = 30 var age:Int = 24 //为变量age设置整型的数据类型 let isEnemy = false var emptyString2 = String() emptyString2.isEmpty 2 print(str) print(“我的名字是(name),今年(age)岁了。”) (name) print(“我的名字是” name ”,今年” age ”岁了。”) if(score>60) { } 字符串拼接:let FullName = firstName ” “ “Lee” //结果为:Jerry Lee 3 整数处理 1 var intNumber = 30 2 intNumber.distance(to:40) //获得两个数据的差值,结 果:10 3 intNumber.advanced(by:20) //将整数增加20,结果:50 4 number.isLess(than:-9.0) 5 number.multiply(by:2)
4 字符串比较 if(passwordFromUser == passwordFromDb) {
}
字符串开头结尾: print(userType1.hasPrefix(“京东”)) //true print(userType2.hasSuffix(“买家”)) //true
5 字符串截取和替换 1 var hello = “Hello, Swift!” 2 hello.substring(from:hello.index(hello.startIndex, offsetBy:7)) //Swift! 3 hello.substring(to:hello.index(hello.endIndex, offsetBy:-8)) //Hello 4 hello.replacingOccurrences(of:“Hello”, with:“Hi”) //Hi, Swift! 5 hello.remove(at:hello.index(hello.startIndex, offsetBy:5)) //, 6 print(hello) //Hello Swift!
6 迭代for 1 var num = 0 2 let hello = “15011123451” 3 for temp in hello.characters 4 { 5 if temp == “1” 6 { 7 num = 1 8 } 9 } 10 print(num) //结果为:5
7 元组 字典 let people = (“John”, 33) 1 let people = (name:“John”, age:33) 2 print(“People’s name is (people.name), and age is (people.age).”)
(people.name)
8 1 let one = 1 2 let minusOne = -one //结果为:-1 3 let plusOne = one //结果为:1 4 let anotherOne = -minusOne //结果为:1
9 循环: for index in 0 ..<3 1 for index in 0 ..< 3 2 { 3 print(“index is (index)”) 4 }
1 let students = [“Jerry”, “Thomas”, “John”] 2 for student in students { 3 print(“Student name:(student)”) 4 }
1 let scores = [“Jerry”:78, “Thomas”:88, “John”:92] 2 for (student, score) in scores 3 { 4 print(student ”’ score is (score)”) 5 }
10 fi 3 if (count!= 18 && count!= 15) 4 { 5 print(“错误的身份证号码。”) 6 }
if else if else
11 switch 1 let time = 12 2 switch time 3 { 4 case 7: 5 print(“It’s time to get up.”) 6 case 8, 12, 18: 7 print(“It’s time for eating.”) 8 case let x where x>18 && x<=24: 9 print(“Happy time.”) 10 case 1……6: 11 print(“It’s time for rest.”) 12 default: 13 print(“Keep busy.”) 14 }
12 数组 1 let stringArray = Array<String>() 2 let floatArray = Float 3 let intArry = [1, 2, 3, 4, 5]
1 let intArry = [1, 2, 3, 4, 5] 2 print(intArry[2]) //结果为:3
1 intArry.isEmpty //判断数组是否为空,结果为:false 2 intArry.count //获得数组中元素的数量,结果为:5 3 intArry.contains(3) //判断数组中是否存在3,结果为:true
1 var intArry = [1, 2, 3, 4, 5] 2 intArry = [6] //intArray的元素:[1, 2, 3, 4, 5, 6] 3 intArry.append(7) //intArray的元素:[1, 2, 3, 4, 5, 6, 7] 4 intArry.insert(100, at:2) // intArray的元素:[1, 2, 100,3, 4, 5, 6, 7]
1 intArry.removeFirst() // intArray的元素:[2, 9, 8, 8, 8,6, 7] 2 intArry.removeLast() // intArray的元素:[2, 9, 8, 8, 8,6] 3 intArry.remove(at:2) // intArray的元素:[2, 9, 8, 8,6] 4 intArry.removeSubrange(Range(1..<3)) // intArray的元素:[2, 8, 6] 5 intArry.removeAll() // intArray的元素:[]
13 排序: intArray.sort() intArry.sort(isOrderedBefore:>)
字典排序
1 var userList = [(name:” Thomas”, age:20), (name:”John”, age:32), (name:” Bill”, age:28)] 2 userList.sort(isOrderedBefore:{
1.name})
14 zip 2 let range = 2..<numbers.count 3 for (index, value) in zip(range, numbers[range]) 4 { 5 print(“(index):(value)”) 6 }
1 var diallingCodes1 = Dictionary<String, String>() 2 var diallingCodes2 = [“010”:”北京”, “021”:”上海”,“0592”:”厦门”]
字典更新
diallingCodes.updateValue(“厦门”, forKey:“0592”)
删除值
1 diallingCodes[“020”] = nil 2 diallingCodes.removeValue(forKey:“020”)