这篇文章介绍了Swift语言中的一些基础知识,包括属性观察器、下标语法、类与结构体区别、Any类型和AnyObject类型、类的继承、向下类型转换、重写方法、final关键字、多态的调用、类的扩展、泛型、协议、类型判断处理、判断两个对象是否相等、延迟属性、可失败的初始化器等。
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
投诉 补充一下Swift的相关知识。
属性观察器 willSet , didSet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import Cocoastruct Person { var name: String = "unknown" { willSet (new_Value){ print ("new value is \(new_Value) " ) }didSet (old_Value){ print ("old value is \(old_Value) " ) } } } var a = Person (name: "Bob" )a.name = "XiaoMing"
下标语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 struct Goods { private var goodsList: [String ] = ["手机" ,"电脑" ,"饭盒" ,"盒饭" ] subscript (goodNum : Int ) -> String { set { goodsList.insert(newValue, at: goodNum) } get { return goodsList[goodNum] } } } var shop = Goods ()shop[1 ] = "水杯" print (shop[1 ])struct Book { private var bookAuthor = [String ]() subscript (rank : Int ) -> String { set (authorName){ bookAuthor.insert(authorName, at: rank) } get { return bookAuthor[rank] } } } var swiftTest = Book ()swiftTest[0 ] = "Hello world" swiftTest[1 ] = "People" print (swiftTest[0 ])
类与结构体区别 类是传引用,结构体是传值。常量或变量的类的实例不影响类的修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 class ClassStudent { var name: String = "unknown" var age: Int = 0 var score: Double = 0.0 init (name : String , age : Int , score : Double ) { self .name = name self .age = age self .score = score } } struct StructStudent { var name: String = "unknown" var age: Int = 0 var score: Double = 0.0 init (name : String , age : Int , score : Double ) { self .name = name self .age = age self .score = score } } var a = ClassStudent (name: "小A" , age: 10 , score: 12 )var b = StructStudent (name: "小B" , age: 49 , score: 60 )var c = aprint (a.name)print (c.name)c.name = "小C" print (a.name)print (c.name)var d = bprint (b.name)print (d.name)d.name = "小D" print (b.name)print (d.name)
Any类型和AnyObject类型 1 2 3 4 5 6 7 8 9 10 11 12 13 class A { } struct B { } var c: Any = A () var d: Any = "hahaha" var e: AnyObject = A ()
类的继承 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import Cocoaclass A { private var name: String = "unknown" func getName () -> String { return name } func setName (newName : String ) { self .name = newName } } class B : A { func play (times : Int ) { print (times) } } var c = A ()print (c.getName())var d = B ()print (d.getName())d.play(times: 3 )
向下类型转换as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import Cocoaclass A { private var name: String = "unknown" func getName () -> String { return name } func setName (newName : String ) { self .name = newName } } class B : A { func play (times : Int ) { print (times) } } var a: A = B ()var b: AnyObject = B ()print (b is B )var c = b as? B var d = b as! []B
重写方法override 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 class Person { var name = "unknown" var age = 0 func getName () -> String { return self .name } init (name : String , age : Int ) { self .name = name self .age = age } } class Student : Person { override var name: String { set { super .name += " - student" }get { return super .name } } override func getName () -> String { return super .name + " - studentfunc" } override init (name : String , age : Int ) { super .init (name: name, age: age) self .name = name } } var a = Person (name: "小A" , age: 12 )print (a.name)var b = Student (name: "小B" , age: 14 )print (b.name)print (b.getName())
final禁止继承和重写方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 final class Person { var name = "unknown" var age = 0 func getName () -> String { return self .name } init (name : String , age : Int ) { self .name = name self .age = age } }
final禁止重写方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Person { var name = "unknown" var age = 0 final func getName () -> String { return self .name } init (name : String , age : Int ) { self .name = name self .age = age } } class Student : Person { override var name: String { set { super .name += " - student" }get { return super .name } } override init (name : String , age : Int ) { super .init (name: name, age: age) self .name = name } } var a = Person (name: "小A" , age: 12 )print (a.name)var b = Student (name: "小B" , age: 14 )print (b.name)
多态的调用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class A { var name: String = "unknown" func getName () -> String { return self .name } init (name : String ) { self .name = name } } class B : A { override func getName () -> String { return super .name + " - B" } func play () { print ("world" ) } } var a: A = B (name: "hello" )print (a.getName())
类的扩展extension 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import Cocoaclass A { } extension A { var name: String {return "hello,world" } func make () { print ("this is A" ) } } var a = A ()a.make() print (a.name)import Cocoaextension String { func pcount () -> String { return "长度 = " + String (self .count) } } var a = "hello" print (a.pcount())
泛型 可以用一个代码来代替各种类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 func todoit <T >(param : T ) -> T { return param } print (todoit(param: "hello" ))print (todoit(param: 123 ))print (todoit(param: [1 ,2 ,3 ]))print (todoit(param: 1.1 ))
协议protocol 类必须要遵守协议
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import Cocoaclass A { } protocol Protocol1 { var a1: String { get set } func b1 () -> String } protocol Protocol2 { var a2: String { get } func b2 () -> String } class B : A , Protocol1 , Protocol2 { var a1: String var a2: String func b1 () -> String { return a1 } func b2 () -> String { return "hello world" } init (a : String ) { self .a1 = a self .a2 = a } } var a = B (a: "swift" )print (a.a1)print (a.a2)print (a.b1())print (a.b2())
类型判断处理if A is B 在多个子类做判断时利用is语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 class A { } class B : A { var name: String = "unknown" func printName () { print (self .name + " - B" ) } init (name : String ) { self .name = name } } class C : A { var name: String = "unknown" func printName () { print (self .name + " - C" ) } init (name : String ) { self .name = name } } func calam (num : Int ) -> A { if num > 10 { return B (name: "小B" ) }else { return C (name: "小C" ) } } var n = calam(num: 20 )if (n is B ) { var p = n as! []B p.printName() }else if (n is C ) { var p = n as! []C p.printName() } if let t = n as? B { t.printName() }else if let t = n as? C { t.printName() }
判断两个对象是否相等=== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class A { } var a = A ()var b = A ()print (a === b)print (a !== b)
延迟属性lazy 延迟属性可以只在用到属性时进行实例化,可以优化性能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import Cocoaclass A { init () { print ("A is run" ) } func play () { print ("A is play" ) } } class B { lazy var a: A = A () init () { print ("B is run" ) } } var a = B ()a.a a.a.play()
可失败的初始化器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import Cocoaclass A { var name: String init? (name : String ) { if name == "world" { return nil } self .name = name } } var t1: A ? = A (name: "hello" )var t2: A ? = A (name: "world" )if let a = t1 { print (a.name) }else { print ("初始化失败" ) } if let a = t2 { print (a.name) }else { print ("初始化失败" ) }
参考资料 Swift编程基础