第九节:结构体-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

源码下载地址:

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

https://github.com/pheromone/swift3

swift系统学习第三章的更多相关文章

  1. Java基础知识二次学习--第三章 面向对象

    第三章 面向对象   时间:2017年4月24日17:51:37~2017年4月25日13:52:34 章节:03章_01节 03章_02节 视频长度:30:11 + 21:44 内容:面向对象设计思 ...

  2. Struts2框架学习第三章——Struts2基础

    本章要点 —  Struts 1框架的基本知识 — 使用Struts 1框架开发Web应用 —  WebWork框架的基本知识 — 使用WebWork框架开发Web应用 — 在Eclipse中整合To ...

  3. C#高级编程 (第六版) 学习 第三章:对象和类型

    第三章 对象和类型 1,类和结构 类存储在托管堆上 结构存储在堆栈上   2,类成员 类中的数据和函数称为类成员 数据成员 数据成员包括了字段.常量和事件   函数成员 方法:与某个类相关的函数,可以 ...

  4. swift系统学习第一章

    第一节:变量,常量,类型推断,字符,字符串 //swift学习第一节 /* 变量 常量 类型推断 字符 字符串 */ import UIKit //变量 var str = "swift&q ...

  5. swift系统学习第二章

    第五节:可选类型 optional //: Playground - noun: a place where people can play import UIKit /* Swift学习第五节 可选 ...

  6. 深度学习框架PyTorch一书的学习-第三章-Tensor和autograd-1-Tensor

    参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 Tensor Tensor可以是一个数 ...

  7. 深度学习框架PyTorch一书的学习-第三章-Tensor和autograd-2-autograd

    参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 torch.autograd就是为了方 ...

  8. swift系统学习控件篇:UITableView+UICollectionView

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...

  9. swift系统学习控件篇:UIProgressView+NSTimer+UIstepper+UIAlertController

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIProgressView+NSTimer+UIstepper UIStepper UIP ...

随机推荐

  1. cf(#div1 B. Dreamoon and Sets)(数论)

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. java 分析方法调用过程

    StackTraceElement[] s = new Exception().getStackTrace(); for(int i=0;i<s.length;i++) System.out.p ...

  3. PHP+socket游戏数据统计平台发包接包类库

    <?php /** * @title: PHP+socket游戏数据统计平台发包接包类库 * @version: 1.0 * @author: perry <perry@1kyou.com ...

  4. Ubuntu用户相关基本命令

    Linux是一个用户权限管理得很严格的系统,Ubuntu作为最受欢迎的桌面发行版,提供了简单易用的图形界面工具来管理用户,但是命令行工具往往更强大,用得熟练的话效率会更高.用户管理命令常用的有如下几个 ...

  5. Python的神奇方法指南

    参考:http://article.yeeyan.org/view/311527/287706

  6. 使用AlarmManager设置闹钟----之一

    import java.util.Calendar; import android.os.Bundle;import android.app.Activity;import android.app.A ...

  7. CAP Confusion: Problems with ‘partition tolerance’

    by Henry Robinson, April 26, 2010 The 'CAP' theorem is a hot topic in the design of distributed data ...

  8. tortoisegit教程

    tortoisegit教程: http://www.mamicode.com/info-detail-311565.html https://my.oschina.net/longxuu/blog/1 ...

  9. oracle遇到死锁杀进程

    java程序操作数据库的时候,遇到死锁:java.sql.SQLException: ORA-00060: 等待资源时检测到死锁 解决步骤: 1.找到死锁的oralce对象(表): select ob ...

  10. js获取URL地址中的GET参数

    var $_GET = (function(){ var url = window.document.location.href.toString(); var u = url.split(" ...