您的位置:首页 > 移动开发 > Swift

swift系统学习第三章

2016-03-23 16:46 417 查看
第九节:结构体-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 = 100
var y : Double = 200

//结构体初始化
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: 100, y: 200)
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, 2) + pow(distY,2))
return dist < distance
}
inRange(pointA)

func move(dist:Double,inout point:Location){
point.x += dist
}
move(100, point: &pointA)
pointA
pointA.move2(100.0)

//extension 扩展
extension Location {
mutating func move3(dist:Double){
self.y += y
}
}
pointA.move3(100)

extension String {
func isEven() -> Bool {
return self.characters.count % 2 == 0 ? 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: 0, y: 1)
let p2 = PointRef(x: 0, y: 2)
let p3 = PointRef(x: 0, y: 2)
// p1.x = 10
p2.x = 10    //这里,p1不能修改,但是p2可以修改.因为p2是引用类型,p1是值类型
if p2 === p3 {
print("他们是相同的对象")
}
if p2 !== p3{
print("他们不是相同的对象")
}

var p4 = p1
p4.x = 10
p1.x

var p5 = p2
p5.x = 10
p2.x


第十一节:
函数式
内存泄露:循环引用

//: Playground - noun: a place where people can play

import UIKit
/*
swift学习第十一节
函数式
内存泄露:循环引用

*/
var number = [1,2,3,4,5,6]
var event = [Int]()
for n in number {
if n % 2 == 0{
event.append(n)
}
}
event

let evens1 = number.filter({(n:Int) -> Bool in return n % 2 == 0})
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: "11")

//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: "11")

tom!.apartment = apt11   //这两句会造成循环引用
apt11!.tenant  = tom

tom = nil
apt11 = nil


源码下载地址:

http://download.csdn.net/detail/shaoting19910730/9470584

https://github.com/pheromone/swift3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: