I'm very new to Swift and programming in general, a bit of Fortran 77 way back, and more recently some simple programming of microcontrollers. I'm working through the basics and all was well until i came across something that i just can't quite get to grips with - delegates. All the online posts don't quite get the concept across, at least for me, so to give myself something that i can refer back to, i've set up a basic template shown below in playground. If i run the code below it works and prints "Something done" to the terminal, but if i make the protocol a "class" protocol ie "protocol SomeDelegate: class {" and make the "var delegate" a "weak var delegate" as recommended in various posts, it doesn't work - what am i doing wrong?
我对Swift和编程非常陌生,有点像Fortran 77,以及最近一些简单的微控制器编程。我正在研究基础知识,一切顺利,直到我遇到了一些我无法理解的事情 - 代表们。所有的在线帖子都没有完全理解这个概念,至少对我而言,所以给自己一些我可以参考的东西,我已经在操场上设置了如下所示的基本模板。如果我运行下面的代码,它工作并向终端输出“Something done”,但是如果我使协议成为“类”协议,即“协议SomeDelegate:class {”并使“var delegate”成为“弱var委托”按照各种帖子的建议,它不起作用 - 我做错了什么?
import UIKit
protocol SomeDelegate {
func DoSomething()
}
class MyViewcontroller: UIViewController, SomeDelegate {
func DoSomething() {
print("Something done")
}
}
class OtherClass {
var delegate: SomeDelegate?
func DoSomething() {
delegate?.DoSomething()
}
}
var myVar = OtherClass()
myVar.delegate = MyViewcontroller()
myVar.DoSomething()
1 个解决方案
#1
3
It doesn't print because the delegate is nil right after you set it. The reason for this is simple: no instance owns it (the reference count is zero). No one owns delegate
because you declared it a weak property of OtherClass. Try establishing an ownership, e.g.
它不会打印,因为代理在设置后就是零。原因很简单:没有实例拥有它(引用计数为零)。没有人拥有委托,因为你声明它是OtherClass的弱属性。尝试建立所有权,例如
var myVar = OtherClass()
let viewController = MyViewController()
myVar.delegate = viewController
Even though delegate is weak, it will now print Something done
again.
虽然代表很弱,但现在它将再次打印出一些东西。
Declaring delegates as weak makes sense because it prevents circular references causing delegate to never be release in memory – that's a whole different story though – check how reference counting works, then you will understand why this is a good practice.
将委托声明为弱是有道理的,因为它会阻止循环引用导致委托永远不会在内存中释放 - 尽管这是一个完全不同的故事 - 检查引用计数的工作原理,然后你就会明白为什么这是一个好的做法。
#1
3
It doesn't print because the delegate is nil right after you set it. The reason for this is simple: no instance owns it (the reference count is zero). No one owns delegate
because you declared it a weak property of OtherClass. Try establishing an ownership, e.g.
它不会打印,因为代理在设置后就是零。原因很简单:没有实例拥有它(引用计数为零)。没有人拥有委托,因为你声明它是OtherClass的弱属性。尝试建立所有权,例如
var myVar = OtherClass()
let viewController = MyViewController()
myVar.delegate = viewController
Even though delegate is weak, it will now print Something done
again.
虽然代表很弱,但现在它将再次打印出一些东西。
Declaring delegates as weak makes sense because it prevents circular references causing delegate to never be release in memory – that's a whole different story though – check how reference counting works, then you will understand why this is a good practice.
将委托声明为弱是有道理的,因为它会阻止循环引用导致委托永远不会在内存中释放 - 尽管这是一个完全不同的故事 - 检查引用计数的工作原理,然后你就会明白为什么这是一个好的做法。