第九节:结构体-sturt
1 //: Playground - noun: a place where people can play 2 3 import UIKit 4 5 /* 6 swift学习第九节 7 结构体:sturt 8 */ 9 let centerX = 100.0 10 let centerY = 100.0 11 let distance = 200.0 12 //定义结构体sturt 13 struct Location { 14 var x : Double = 100 15 var y : Double = 200 16 17 //结构体初始化 18 init (StringPoint:String){ 19 let xy = StringPoint.characters.split(",") 20 x = atof(String(xy.first!)) 21 y = atof(String(xy.last!)) 22 23 } 24 init(x:Double,y:Double){ 25 self.x = x 26 self.y = y 27 } 28 init(){} 29 30 mutating func move2(dist:Double){ 31 self.x += dist 32 33 } 34 35 } 36 37 var pointA = Location(x: 100, y: 200) 38 let pointB = Location(StringPoint: "100,200") 39 let PointC = Location() 40 pointA.x 41 pointA.y 42 pointB.x 43 pointB.y 44 45 //把结构体作为参数传入 46 func inRange(point:Location) -> Bool { 47 let distX = point.x - centerX 48 let distY = point.y - centerY 49 let dist = sqrt(pow(distX, 2) + pow(distY,2)) 50 return dist < distance 51 } 52 inRange(pointA) 53 54 func move(dist:Double,inout point:Location){ 55 point.x += dist 56 } 57 move(100, point: &pointA) 58 pointA 59 pointA.move2(100.0) 60 61 //extension 扩展 62 extension Location { 63 mutating func move3(dist:Double){ 64 self.y += y 65 } 66 } 67 pointA.move3(100) 68 69 extension String { 70 func isEven() -> Bool { 71 return self.characters.count % 2 == 0 ? true : false 72 } 73 } 74 "An even string".isEven() 75 76 var copyPointA = pointA 77 copyPointA.y = 10000.0 78 pointA // struct 是值类型
第十节:
struct 和 class
1. class 必须有init()方法
struct 可以不写init()方法,默认有init方法
2. struct 为值类型
class 为引用类型
1 //: Playground - noun: a place where people can play 2 /* 3 swift学习第十节 4 struct 和 class 5 1. class 必须有init()方法 6 struct 可以不写init()方法,默认有init方法 7 2. struct 为值类型 8 class 为引用类型 9 10 */ 11 import UIKit 12 13 struct PointVal { 14 var x: Int 15 var y: Int 16 init(x:Int,y:Int){ 17 self.x = x 18 self.y = y 19 } 20 mutating func moveX(x:Int){ 21 self.x += x 22 } 23 } 24 class PointRef { 25 var x: Int 26 var y: Int 27 init(x:Int,y:Int){ 28 self.x = x 29 self.y = y 30 } 31 func moveX(x:Int){ 32 self.x += x 33 } 34 } 35 let p1 = PointVal(x: 0, y: 1) 36 let p2 = PointRef(x: 0, y: 2) 37 let p3 = PointRef(x: 0, y: 2) 38 // p1.x = 10 39 p2.x = 10 //这里,p1不能修改,但是p2可以修改.因为p2是引用类型,p1是值类型 40 if p2 === p3 { 41 print("他们是相同的对象") 42 } 43 if p2 !== p3{ 44 print("他们不是相同的对象") 45 } 46 47 var p4 = p1 48 p4.x = 10 49 p1.x 50 51 var p5 = p2 52 p5.x = 10 53 p2.x
第十一节:
函数式
内存泄露:循环引用
1 //: Playground - noun: a place where people can play 2 3 import UIKit 4 /* 5 swift学习第十一节 6 函数式 7 内存泄露:循环引用 8 9 */ 10 var number = [1,2,3,4,5,6] 11 var event = [Int]() 12 for n in number { 13 if n % 2 == 0{ 14 event.append(n) 15 } 16 } 17 event 18 19 let evens1 = number.filter({(n:Int) -> Bool in return n % 2 == 0}) 20 evens1 21 22 //-------------------------------内存管理-------------------- 23 class Person { 24 let name:String 25 init(name:String){ 26 self.name = name 27 print("(name) 被构建") 28 } 29 deinit{ 30 print("(name) 被销毁") 31 } 32 } 33 var ref1:Person? 34 var ref2:Person? 35 36 ref1 = Person(name: "Tom") //Person对象的引用计数为1 37 ref2 = ref1 //Person对象的引用计数为2 38 ref1 = nil //Person对象的引用计数为1 39 ref2 = nil //deinit()方法被执行 40 41 //循环引用造成的内存泄露 42 class Student { 43 let name:String 44 var apartment:School? 45 init(name:String){ 46 self.name = name 47 print("(name)被创建") 48 } 49 deinit{ 50 print("(name)被销毁"); 51 } 52 } 53 class School { 54 let unit: String 55 var tenant: Student? 56 init(unit:String){ 57 self.unit = unit 58 print("(unit)被创建") 59 } 60 deinit{ 61 print("(unit)被销毁") 62 } 63 } 64 var tom:Student? = Student(name: "TOM") 65 var apt11:School? = School(unit: "11") 66 67 //tom!.apartment = apt11 //这两句会造成循环引用 68 //apt11!.tenant = tom 69 70 tom = nil 71 apt11 = nil
第十二节:
reference cycle 的方式
1.使用weak 使用weak,不会造成引用计数+1,如果没有指向任何对象,则自动设置为nil
场景:双方均可以为空
2.umowned
场景:一方可以为空
1 //: Playground - noun: a place where people can play 2 /* 3 reference cycle 的方式 4 1.使用weak 使用weak,不会造成引用计数+1,如果没有指向任何对象,则自动设置为nil 5 场景:双方均可以为空 6 2.umowned 7 场景:一方可以为空 8 9 */ 10 11 12 import UIKit 13 14 //循环引用造成的内存泄露 15 class Student { 16 let name:String 17 var apartment:School? 18 init(name:String){ 19 self.name = name 20 print("(name)被创建") 21 } 22 deinit{ 23 print("(name)被销毁"); 24 } 25 } 26 class School { 27 let unit: String 28 weak var tenant: Student? 29 init(unit:String){ 30 self.unit = unit 31 print("(unit)被创建") 32 } 33 deinit{ 34 print("(unit)被销毁") 35 } 36 } 37 var tom:Student? = Student(name: "TOM") 38 var apt11:School? = School(unit: "11") 39 40 tom!.apartment = apt11 //这两句会造成循环引用 41 apt11!.tenant = tom 42 43 tom = nil 44 apt11 = nil
源码下载地址: