在swift中检查JSON的解析器时,我发现了以下代码:
description = "desc" <~~ json
Run Code Online (Sandbox Code Playgroud)
我想它类似于使用以下内容:
description = json["desc"]
Run Code Online (Sandbox Code Playgroud)
这是对的吗?如果不是,这个算子是什么意思?
谢谢
将闭包定义为变量时,我有一个保留周期。
变量定义如下:
public class MyCell: UICollectionViewCell {
public var callback: ((MyCell)->Void)?
}
Run Code Online (Sandbox Code Playgroud)
如果我使用委托而不是闭包,保留周期就会消失,但我想知道如何用闭包来定义它以备将来使用。
我试图将回调变量设置为weak,但是,我想,弱属性只能应用于类和类绑定协议类型。
编辑
用法:
class CustomController: UIViewController {
private func onActionOccurs(_ cell: MyCell) {
cell.backgroundColor = .red // For example
}
// After dequeuing the cell:
{
cell.callback = onActionOccurs(_:)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢