_swift_abortRetainUnowned when capturing the @objc class as unowned

I experience this strange crash when capturing an instance of the @objc class (written fast but annotated with @objc and a subclass of NSObject ). This happens when a closure is assigned, not when it is called, so the problem is not that the captured value will be released, and then the closure is called. This happens randomly, sometimes it falls earlier, sometimes later. I experienced this error in earlier versions of Swift (I think it was Swift 1.2), but now I am using 2.1 and getting this crash too.

It works well when I change [unowned x] to [weak x] and then click on it, unpacking x!.doSomething() , which makes me think that this is a bug in Swift, not in my code. However, before opening a ticket on bugs.swift.org, I wanted to know more about this to make sure that I did not miss anything.

It was also mentioned here:
http://www.codeproject.com/Articles/791304/Resolving-strong-references-between-Swift-and-Obje
here:
https://www.reddit.com/r/swift/comments/3vhwmj/unowned_bug_in_closure_causes_attempted_to_retain/
and here:
https://forums.developer.apple.com/thread/9873

But apart from changing the capture to weak and forcibly deploying it later, there is no solution.

+6
source share
1 answer

I usually prefer to use this code instead of unowned when I have to do with some objc classes included:

 [weak self] in guard let strongSelf = self else { return } // use strongSelf below 
+2
source

All Articles