小胖说swift07-------- swift协议代理的使用以及解决循环引用问题

小胖说swift07-------- swift协议代理的使用以及解决循环引用问题,第1张

概述这两天看了一下Swift的协议代理, 大体思路和OC没什么区别, 但是按照官方的书本写出的协议代理, 发现会有内存泄露问题, 找了半天没有发现问题, 突然想起看系统类的协议代理的写法, 瞬间发现了问题, 不多废话了, 下面是代码.  我建了一个single View application, 为了验证内存泄露, 我保留了xcode所给的viewController.swift,在storyboar

这两天看了一下Swift的协议代理,大体思路和OC没什么区别,但是按照官方的书本写出的协议代理,发现会有内存泄露问题,找了半天没有发现问题,突然想起看系统类的协议代理的写法,瞬间发现了问题,不多废话了,下面是代码.

我建了一个single VIEw application,为了验证内存泄露,我保留了xcode所给的vIEwController.swift,在storyboard中vIEwController前添加了一个navigationController,并且自己建了两个视图控制器: VC1.swift 和 VC2.swift,由vIEwController 跳转至 VC1,在VC1中设置delegate,再跳转到VC2,让VC2改变VC1的背景颜色,然后一直返回到vIEwController,看VC1 和 VC2 是否释放了内存;

下面是我的vIEwController里的代码:

////  VIEwController.swift//  protocolTest////  Created by XXX on 16/8/26.//  copyright © 2015年 V1. All rights reserved.//import UIKitclass VIEwController: UIVIEwController {    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        self.Title = "vIEwController"        //实例化一个button,添加点击事件        let button = UIbutton(type: UIbuttonType.Custom)        button.backgroundcolor = UIcolor.redcolor()        button.setTitle("点击跳转到VC1",forState: UIControlState.normal)        button.frame = CGRectMake(100,100,150,100)        button.addTarget(self,action: "buttonClicked",forControlEvents: UIControlEvents.touchUpInsIDe)        self.vIEw.addSubvIEw(button)    }    //跳转到VC1    func buttonClicked(){        let vc_1 = VC1()        self.navigationController?.pushVIEwController(vc_1,animated: true)    }}

这下面是VC1 中的代码:

////  VC1.swift//  protocolTest////  Created by XXX on 16/8/26.//  copyright © 2015年 V1. All rights reserved.//import UIKit// VC1 遵守changecolor协议,并且实现协议中的方法class VC1: UIVIEwController,changecolor{    var vc_2: VC2?   //定义一个VC2变量    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        self.vIEw.backgroundcolor = UIcolor.orangecolor()        self.Title = "VC1"        //实例化VC2,并且设置delegate        vc_2 = VC2()        vc_2?.delegate = self        let button = UIbutton(type: UIbuttonType.Custom)        button.backgroundcolor = UIcolor.whitecolor()        button.setTitle("点击跳转到VC2",forState: UIControlState.normal)        button.setTitlecolor(UIcolor.blackcolor(),action: "VC1buttonClicked",forControlEvents: UIControlEvents.touchUpInsIDe)        self.vIEw.addSubvIEw(button)    }    //跳转到VC2    func VC1buttonClicked(){        self.navigationController?.pushVIEwController(vc_2!,animated: true)    }    //实现协议中的方法    func changecolorWithcolor(color color: UIcolor) {        self.vIEw.backgroundcolor = color    }    deinit{        print("vc_1 的内存已释放 !")    }}

下面是我的VC2中的代码:

////  VC2.swift//  protocolTest////  Created by XXXX on 16/8/26.//  copyright © 2015年 V1. All rights reserved.//import UIKit//声明一个协议,让其继承(我也不知道该不该叫继承,然而在这里并不重要) NSObjectProtocol,只有这样才能在设置代理的时候前面添加weakprotocol changecolor: NSObjectProtocol {    func changecolorWithcolor(color color: UIcolor)}class VC2: UIVIEwController {    //注意这里: changecolor为协议名,delegate前面必须有weak修饰,如果没有weak修饰就会造成内存泄露,而可以加weak的前提是,这个协议必须继承 NSObjectProtocol,这是我试验出来的,目前来看,应该是这样的    weak var delegate: changecolor?    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        self.vIEw.backgroundcolor = UIcolor.whitecolor()        self.Title = "VC2"        let button = UIbutton(type: UIbuttonType.Custom)        button.backgroundcolor = UIcolor.redcolor()        button.setTitle("点击改变VC1的背景颜色",200,forControlEvents: UIControlEvents.touchUpInsIDe)        self.vIEw.addSubvIEw(button)    }    func VC1buttonClicked(){        //使用代理调用协议声明并且VC1已经实现的方法        self.delegate?.changecolorWithcolor(color: UIcolor.cyancolor())    }    deinit{        print("vc_2的内存已经释放 !")    }}

下面是返回至vIEwController后的打印信息:

/*vc_1 的内存已释放 !vc_2的内存已释放 !*/

最重要的代码片段为:

//注意这里: changecolor为协议名,应该是这样的

weak var delegate: changecolor?

总结

以上是内存溢出为你收集整理的小胖说swift07-------- swift协议代理的使用以及解决循环引用问题全部内容,希望文章能够帮你解决小胖说swift07-------- swift协议代理的使用以及解决循环引用问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1072879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存