It sounds to me like you're trying to avoid a retain cycle with a block like you do in Objective-C, where instead of referencing self, you create a weak version:
__weak MyType *weakSelf = self;
void (^aBlock)() = ^void()
{
[weakSelf doStuff];
}
That is not how Swift handles this problem.
Instead, it has the concept of a capture list, that tells the compiler which references the block captures, and what to do about it. You should search the Swift Programming Reference book for "Capture List" and read up on the subject. To quote the book:
“If you assign a closure to a property of a class instance, and the
closure captures that instance by referring to the instance or its
members, you will create a strong reference cycle between the closure
and the instance. Swift uses capture lists to break these strong
reference cycles. For more information, see Strong Reference Cycles
for Closures.”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
Edited 4 Jan 2016:
To quote the part of the Swift book that explains how to create a capture list:
Defining a Capture List: Each item in a capture list is a pairing of
the weak or unowned keyword with a reference to a class instance (such
as self) or a variable initialized with some value (such as delegate =
self.delegate!). These pairings are written within a pair of square
braces, separated by commas.
Place the capture list before a closure’s parameter list and return
type if they are provided:
lazy var someClosure: (Int, String) -> String =
{
[unowned self, weak delegate = self.delegate!]
(index: Int, stringToProcess: String) -> String in
// closure body goes here
}
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2).” iBooks. https://itun.es/us/jEUH0.l
weak
reference toself
, if not in the context of blocks/closures? If you want adelegate
property to be weak, for example, you'd just declare it as such (e.g.weak var delegate: MyProtocol!
or whatever). – Nutcrackerself
did not exist? You need an object to run methods on. – Somaliland