- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于 ARC 的教程很多。但我不理解 unowned 或 weak 的明确工作原理,因为引用捕获的变量如何变为 null。
苹果文档:
Define a capture in a closure as an unowned reference when the closure and the instance it captures will always refer to each other, and will always be deallocated at the same time.
class RetainCycle {
var closure: (() -> Void)!
var string = "Hello"
init() {
closure = { [unowned self] in
self.string = "Hello, World!"
}
}
}
闭包在其主体内引用 self(作为引用 self.string 的一种方式),闭包捕获 self,这意味着它持有对 RetainCycle 实例的强引用。两者之间形成了强引用循环。由 unowned 打破引用循环。
但我想了解哪种情况两者不会相互同时解除分配并且 Unowned self 变为 null 只是想让它崩溃。?
最佳答案
据我了解,您会问在 closue 运行时如何将 self 设为 null。如果我做对了,我可以给你一个我以前见过的非常相似的例子。
我写了一个 UIImageView 的扩展,它从给定的链接下载图像并像这样设置自己。
public extension UIImageView{
func downloadImage(link: String){
let url = URL(string:link)
URLSession.shared.dataTask(with: url){ [unowned self]
if let image = UIImage(data: data){
DispatchQueue.main.async{
self.image = image
}
}
}
task.start()
}
}
但是有一个问题。下载图像是一项后台任务。我将完成方法设置为 UrlSession 并增加了它的引用计数。因此,即使 imageView 被取消分配,我的闭包仍然存在。
那么,如果我在下载完成之前关闭保存我的 UIImageView
的 viewController
会发生什么。它崩溃是因为 imageView
被释放,但闭包仍然存在并试图访问它的 image
属性。据我了解,您想学习这个。
我将 unowned
引用更改为 weak
来解决这个问题。
关于swift - Unowned 引用如何与 Swift 中的捕获变量一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55001921/
有什么方法可以检查 unowned(safe) Swift 引用的“可用性”?所以,我在这个例子中寻找一个像 isReferenceAccessible 这样的假设函数: func someMetho
Apple 的 Swift Programming Language Guide除了 weak 和 之外,还提到了捕获说明符 unowned(safe) 和 unowned(unsafe)无主。 我(
在下面的代码中,如果Swift数组是按值传递的,是否必须要有unowned? Category 有一个 Swift 数组的属性,而不是一个 Item,所以 unowned 不是必需的,对吧? fina
基本上我有以下 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITa
我对 swift 中的这个主题感到困惑,据说无主引用必须始终有一个值并且不能是可选的,也意味着它们不能设置为“nil”....好吧,我刚刚看到了一个程序苹果的 Swift 文档表明,实例“A”具有对实
在 Swift 中存在递归内存泄漏问题,其中一个单例在另一个单例的闭包内被调用。 NetworkManager.sharedInstance.doThingWithCompletion(urlStri
以下模式在 iOS 应用中经常发生: class MyViewController: UIViewController { let myModel = MyModel() overri
我正在尝试创建一个闭包来清除我的缓存,这两个都是实例变量。当我尝试调用 [unowned self] 时,出现错误“‘unowned may only be applied to class and
关于 ARC 的教程很多。但我不理解 unowned 或 weak 的明确工作原理,因为引用捕获的变量如何变为 null。 苹果文档: Define a capture in a closure as
我正在尝试集成 Face/Touch ID 登录,我在 Apple 的文档中看到了 [unowned self],在一个闭包内。那是什么,有什么好处? 示例代码: let context = LACo
所以我一直在做一些关于 UIViewPropertyAnimator 的阅读,在我一直在看的例子中,他们做了这样的事情: animator = UIViewPropertyAnimator(durat
这个问题在这里已经有了答案: What is the difference between a weak reference and an unowned reference? (7 个答案) 关闭
我在一个闭包中有一个闭包,第二个闭包使用 self,所以两者都应该有 unowned self 还是只有第二个闭包应该有它? dispatch_async(backgroundQueue) { [un
我有一个保留循环,所以我的 viewcontroller 的 deinit 不会被调用,我试图通过添加 [unowned self] 来解决这个问题,但我不太确定在我的案例中将 unowned 放在哪
WWDC 2014 第 403 场 session Intermediate Swift和 transcript ,有以下幻灯片 演讲者说,在这种情况下,如果我们不使用[unowned self],就
我遇到了这段我不理解的 Swift 代码。分配给 navigationCallback 的是什么?什么是 [unowned self]?如果我能看到 Objective-C 中的等效示例,我想我会更好
对于我添加了 deinitializers 的 swift 编程指南中的以下代码,无论是否使用 unowned 关键字,生成的调试打印输出都是相同的。 swift 编程指南说,当引用彼此类实例的两个属
我有一个带有完成处理程序的函数,返回一个或多个参数。 在客户端中,当执行完成处理程序时,我希望有一个 unowned 对 self 的引用,以及对传递的参数的访问权。 这是说明问题和我要实现的目标的
每当我执行异步网络请求时,可能是在请求到达时 self 已经为 nil(例如,ViewController 已经被关闭)。 为了防止这种情况,我通常将自己描述为软弱的: future.onSu
我正在尝试编写一个简单的闭包作为完成处理程序,并在闭包内设置文本框的文本值: class ViewController: UIViewController { @IBOutlet var te
我是一名优秀的程序员,十分优秀!