Swift ARC和块 [英] Swift ARC and blocks

查看:83
本文介绍了Swift ARC和块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个简单的示例,如下所示:

I'm trying a simple example as seen here: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_88

这是我的代码. (忽略其他可能的代码,这是一个空项目,此代码写在一个空的UIViewcontroller viewDidLoad中)

And this is my code. (Ignore other possible code, this is a empty project with this code written inside an empty UIViewcontroller viewDidLoad)

    dispatch_async(dispatch_get_main_queue()) {
        [unowned self] in
        println(self)
    }

我不明白为什么我在运行专业版时会崩溃

I don't understand why it crashes when I run the pro

  • 线程#1:tid = 0x1a796,0x00284d18 libswiftCore.dylib`_swift_release_slow + 8,队列= 'com.apple.main-thread',停止原因= EXC_BAD_ACCESS(代码= 1, 地址= 0x458bc681)
  • thread #1: tid = 0x1a796, 0x00284d18 libswiftCore.dylib`_swift_release_slow + 8, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x458bc681)

在最新的beta(5)上是否发生了更改,并且不再支持此功能? 谢谢

Did something changed on the latest beta(5) and this is not supported anymore? Thanks

有趣的是,此代码可在Objc上运行

edit: Interesting that this code works on Objc

__weak MyViewController *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"%@", weakSelf);
});

edit2: 关于此链接的说明:我们应始终使用[unown自我]在Swift内关闭中关于弱者和无人者的区别是错误的.

edit2: The explanation on this link : Shall we always use [unowned self] inside closure in Swift on the difference of weak and unowned is wrong.

不仅仅是虚弱的钉子和无主的钉子.如果是这种情况,这也应该崩溃:

It's not just that weak nils and unowned doesn't. If that's the case, this should crash as well:

  dispatch_async(dispatch_get_main_queue()) {
            [weak self] in
            println(self)
        }

但是它没有,并且打印了指针,所以它不是nil.

but it doesn't, and it prints the pointer, so, it's not nil.

推荐答案

[Unowned self]使之关闭,因此闭包不会创建对self的强引用,并且如果将其释放,也不会自动将其设置为nil任何一个.在执行异步方法时,self已被释放.这就是为什么您要崩溃.

[Unowned self] makes it so the closure does not create a strong reference to self and it does not automatically set it to nil if it gets deallocated either. By the time the async method is executed, self has been deallocated. That is why you are getting the crash.

在一次异步调用中使用unowned当然没有意义.最好对此有一个强烈的参考,以确保它坚持下去.由于self不拥有闭包,因此仍然没有强大的参考周期.

It certainly doesn't make sense to use unowned in a one time asynchronous call. It would be better to capture a strong reference to it to be sure it sticks around. There still won't be a strong reference cycle because self does not own the closure.

侧面说明:这不能是您的所有代码,因为self并未在代码中的任何地方定义.

Side Note: This cannot be all of your code as self isn't defined anywhere in your code.

unownedweak是两件事.在Objective-C中,unowned被称为unsafe unretained.您可以使用两种语言的weak. weak意味着如果对象被释放,运行时将自动将引用转换为nil. unownedunsafe unretained意味着您不会将其设置为nil(这就是为什么在Objective-C中将其称为不安全"的原因.

unowned and weak are two different things. In Objective-C, unowned is called unsafe unretained. You can use weak in both languages. weak means that the runtime will automatically convert the reference to nil if the object is deallocated. unowned or unsafe unretained means that it will not be set to nil for you (which is why it is called "unsafe" in Objective-C.

Unowned仅应在永不释放对象的情况下使用.在这种情况下,请使用weak.

Unowned should only ever be used in circumstances where the object will never be deallocated. In those circumstances, use weak.

请记住,如果您在Swift中将变量捕获为weak,则该引用将成为可选的,因此要使用该引用,您必须将其解包:

Keep in mind, that if you capture a variable as weak in Swift, the reference will be made an optional so to use it you will have to unwrap it:

dispatch_async(dispatch_get_main_queue()) {
    [weak self] in
    if let actualSelf == self {
         // do something with actualSelf
    }
    // you can still print the "wrapped" self because it is fine to print optionals
    // even if they are `nil`
    println(self)
}

但是要清楚一点,在这种情况下,最好还是使用强有力的参考:

But to be clear, it would still be best to use a strong reference in this circumstance:

dispatch_async(dispatch_get_main_queue()) {
    println(self)
}

这篇关于Swift ARC和块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆