Apple Swift New Programming Language for Mac & iOS Sen Ma
Function patterns Protocols and extensions on structs Fast iteration Closures Generics Concise syntax Pattern matching Native collections Optional Types Operator overloading Object orientation Tuples Namespaces Type inference Clear mutability syntax Read-Eval-Print-Loop (REPL) Multiple return types Interactive playground Compile to native code
Fast Modern Safe Interactive
#include <iostream> using namespace std; � int main() { cout << "Hello World!" << endl; println("Hello World!") "Hello World!" return 0; }
goto pointers type casting uninitialized variables unsafe string formatting unclear copy / reference rules
Variables var name: type = "value" var version: Double = 1.0 var year: Int = 2014 var LanguageName: String = "Swift"
Constant variables let name: type = "value" let version: Double = 1.0 let year: Int = 2014 let LanguageName: String = "Swift"
Constant variables let name: type = "value" let version: Double = 1.0 let year: Int = 2014 let LanguageName: String = "Swift"
Constant variables let name = "value" let version = 1.0 let year = 2014 let LanguageName = "Swift"
Constant variables let name = "value" // Inference Type let version = 1.0 // Double let year = 2014 // Int let LanguageName = "Swift" // String let π = 3.1415926 // Double let 🐷 = "dog" // String
println let a=3, b=5 Output:
println let a=3, b=5 println(a) println(b) Output:
println let a=3, b=5 println(a) println(b) Output: 3 5
println let a=3, b=5 println(a) println(b) Output: 3 5 3 times 5 is 8
println let a=3, b=5 println(a) println(b) println("\(a) times \(b) is \(a*b)") Output: 3 5 3 times 5 is 8
Demo
Loops for (var i=0; i<10; i++){ } var i=0; while (i<10){ println(i) i++; }
Loops - for in for ch in "mouse"{ println(ch) } Output: m o u s e
Loops - for in for ch in " ⿏鼡标 "{ println(ch) } Output: ⿏鼡 标
Loops - for in for ch in " 🐮🐮🐮🐮🐮 "{ println(ch) } Output: 🐮 🐮 🐮 🐮 🐮
Loops - for in - Range for num in 1…5{ println(num) } Output: 1 2 3 4 5
Loops - for in - Range for num in 0..<5{ println(num) } Output: 0 1 2 3 4
Demo
Array let array = [1, 2, 3, 4, 5] Output:
Array let array = [1, 2, 3, 4, 5] println(array) Output:
Array let array = [1, 2, 3, 4, 5] println(array) Output: [1,2,3,4,5]
Array let array = [1, 2, 3, 4, 5] println(array) println(array[1]) Output: [1,2,3,4,5]
Array let array = [1, 2, 3, 4, 5] println(array) println(array[1]) Output: [1,2,3,4,5] 2
Array let array = [1, 2, 3, 4, 5] for num in array{ println(num) } Output: 1 2 3 4 5
Function func SayHello(){ println("Hello") } SayHello() Output:
Function func SayHello(){ println("Hello") } SayHello() Output: Hello
Function func SayHello(name: String){ println("Hello \(name)") } SayHello("Sen Ma") Output: Hello
Function func SayHello(name: String){ println("Hello \(name)") } SayHello("Sen Ma") Output: Hello Sen Ma
Function func SayHello(name: String)->String{ return "Hello " + name } SayHello("Sen Ma") Output: Hello Sen Ma
Demo
Recommend
More recommend