如何在块外的 swift 中创建指向自身的弱指针
how to make a weak pointer to self in swift outside of a block
我想在 swift 中创建一个指向自身的弱指针,就像我们以前在 objective-c 中那样
__weak Something *weakself = self;
我发现有人在解释如何在块中使用 'weak self',
{ in [unowned self] ...}
但我不想在我的块内定义 'weakself',我想在块外定义 weakself
只需用weak
关键字定义弱引用即可:
weak var weakSelf = self
You indicate a weak reference by placing the weak
keyword before a
property or variable declaration.
...
NOTE: Weak references must be declared as variables, to indicate that their value can change at runtime. A weak reference cannot be declared as a constant.
在我看来你就像在 Objective-C 中那样试图避免使用块的保留循环,你创建了一个弱版本而不是引用自身:
__weak MyType *weakSelf = self;
void (^aBlock)() = ^void()
{
[weakSelf doStuff];
}
这不是 Swift 处理此问题的方式。
相反,它具有捕获列表的概念,它告诉编译器引用块捕获的内容,以及如何处理它。您应该在 Swift 编程参考书中搜索 "Capture List" 并阅读该主题。引用本书:
“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.”
摘自:Apple Inc.“Swift 编程语言。”电子书。 https://itun.es/us/jEUH0.l
2016 年 1 月 4 日编辑:
引用 Swift 书中解释如何创建捕获列表的部分:
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
}
摘自:Apple Inc.“Swift 编程语言 (Swift 2)。”电子书。 https://itun.es/us/jEUH0.l
我想在 swift 中创建一个指向自身的弱指针,就像我们以前在 objective-c 中那样
__weak Something *weakself = self;
我发现有人在解释如何在块中使用 'weak self',
{ in [unowned self] ...}
但我不想在我的块内定义 'weakself',我想在块外定义 weakself
只需用weak
关键字定义弱引用即可:
weak var weakSelf = self
You indicate a weak reference by placing the
weak
keyword before a property or variable declaration.
...
NOTE: Weak references must be declared as variables, to indicate that their value can change at runtime. A weak reference cannot be declared as a constant.
在我看来你就像在 Objective-C 中那样试图避免使用块的保留循环,你创建了一个弱版本而不是引用自身:
__weak MyType *weakSelf = self;
void (^aBlock)() = ^void()
{
[weakSelf doStuff];
}
这不是 Swift 处理此问题的方式。
相反,它具有捕获列表的概念,它告诉编译器引用块捕获的内容,以及如何处理它。您应该在 Swift 编程参考书中搜索 "Capture List" 并阅读该主题。引用本书:
“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.”
摘自:Apple Inc.“Swift 编程语言。”电子书。 https://itun.es/us/jEUH0.l
2016 年 1 月 4 日编辑:
引用 Swift 书中解释如何创建捕获列表的部分:
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
}
摘自:Apple Inc.“Swift 编程语言 (Swift 2)。”电子书。 https://itun.es/us/jEUH0.l