第九节:结构体-sturt
//: Playground - noun: a place where people can play import UIKit /*
swift学习第九节
结构体:sturt
*/
let centerX = 100.0
let centerY = 100.0
let distance = 200.0
//定义结构体sturt
struct Location {
var x : Double =
var y : Double = //结构体初始化
init (StringPoint:String){
let xy = StringPoint.characters.split(",")
x = atof(String(xy.first!))
y = atof(String(xy.last!)) }
init(x:Double,y:Double){
self.x = x
self.y = y
}
init(){} mutating func move2(dist:Double){
self.x += dist } } var pointA = Location(x: , y: )
let pointB = Location(StringPoint: "100,200")
let PointC = Location()
pointA.x
pointA.y
pointB.x
pointB.y //把结构体作为参数传入
func inRange(point:Location) -> Bool {
let distX = point.x - centerX
let distY = point.y - centerY
let dist = sqrt(pow(distX, ) + pow(distY,))
return dist < distance
}
inRange(pointA) func move(dist:Double,inout point:Location){
point.x += dist
}
move(, point: &pointA)
pointA
pointA.move2(100.0) //extension 扩展
extension Location {
mutating func move3(dist:Double){
self.y += y
}
}
pointA.move3() extension String {
func isEven() -> Bool {
return self.characters.count % == ? true : false
}
}
"An even string".isEven() var copyPointA = pointA
copyPointA.y = 10000.0
pointA // struct 是值类型
第十节:
struct 和 class
1. class 必须有init()方法
struct 可以不写init()方法,默认有init方法
2. struct 为值类型
class 为引用类型
//: Playground - noun: a place where people can play
/*
swift学习第十节
struct 和 class
1. class 必须有init()方法
struct 可以不写init()方法,默认有init方法
2. struct 为值类型
class 为引用类型 */
import UIKit struct PointVal {
var x: Int
var y: Int
init(x:Int,y:Int){
self.x = x
self.y = y
}
mutating func moveX(x:Int){
self.x += x
}
}
class PointRef {
var x: Int
var y: Int
init(x:Int,y:Int){
self.x = x
self.y = y
}
func moveX(x:Int){
self.x += x
}
}
let p1 = PointVal(x: , y: )
let p2 = PointRef(x: , y: )
let p3 = PointRef(x: , y: )
// p1.x = 10
p2.x = //这里,p1不能修改,但是p2可以修改.因为p2是引用类型,p1是值类型
if p2 === p3 {
print("他们是相同的对象")
}
if p2 !== p3{
print("他们不是相同的对象")
} var p4 = p1
p4.x =
p1.x var p5 = p2
p5.x =
p2.x
第十一节:
函数式
内存泄露:循环引用
//: Playground - noun: a place where people can play import UIKit
/*
swift学习第十一节
函数式
内存泄露:循环引用 */
var number = [,,,,,]
var event = [Int]()
for n in number {
if n % == {
event.append(n)
}
}
event let evens1 = number.filter({(n:Int) -> Bool in return n % == })
evens1 //-------------------------------内存管理--------------------
class Person {
let name:String
init(name:String){
self.name = name
print("\(name) 被构建")
}
deinit{
print("\(name) 被销毁")
}
}
var ref1:Person?
var ref2:Person? ref1 = Person(name: "Tom") //Person对象的引用计数为1
ref2 = ref1 //Person对象的引用计数为2
ref1 = nil //Person对象的引用计数为1
ref2 = nil //deinit()方法被执行 //循环引用造成的内存泄露
class Student {
let name:String
var apartment:School?
init(name:String){
self.name = name
print("\(name)被创建")
}
deinit{
print("\(name)被销毁");
}
}
class School {
let unit: String
var tenant: Student?
init(unit:String){
self.unit = unit
print("\(unit)被创建")
}
deinit{
print("\(unit)被销毁")
}
}
var tom:Student? = Student(name: "TOM")
var apt11:School? = School(unit: "") //tom!.apartment = apt11 //这两句会造成循环引用
//apt11!.tenant = tom tom = nil
apt11 = nil
第十二节:
reference cycle 的方式
1.使用weak 使用weak,不会造成引用计数+1,如果没有指向任何对象,则自动设置为nil
场景:双方均可以为空
2.umowned
场景:一方可以为空
//: Playground - noun: a place where people can play
/*
reference cycle 的方式
1.使用weak 使用weak,不会造成引用计数+1,如果没有指向任何对象,则自动设置为nil
场景:双方均可以为空
2.umowned
场景:一方可以为空 */ import UIKit //循环引用造成的内存泄露
class Student {
let name:String
var apartment:School?
init(name:String){
self.name = name
print("\(name)被创建")
}
deinit{
print("\(name)被销毁");
}
}
class School {
let unit: String
weak var tenant: Student?
init(unit:String){
self.unit = unit
print("\(unit)被创建")
}
deinit{
print("\(unit)被销毁")
}
}
var tom:Student? = Student(name: "TOM")
var apt11:School? = School(unit: "") tom!.apartment = apt11 //这两句会造成循环引用
apt11!.tenant = tom tom = nil
apt11 = nil
源码下载地址: