好的,我们有UIScrollView
声明:
protocol UIScrollViewDelegate: NSObjectProtocol { ... }
class UIScrollView: UIView {
...
weak var delegate: UIScrollViewDelegate?
...
}
Run Code Online (Sandbox Code Playgroud)
然后UITableView
用delegate
变种?
protocol UITableViewDelegate: NSObjectProtocol, UIScrollViewDelegate { ... }
class UITableView: UIScrollView {
...
weak var delegate: UITableViewDelegate?
...
}
Run Code Online (Sandbox Code Playgroud)
Apple如何做到这一点?当我做我的
protocol MyScrollViewSubclassDelegate: NSObjectProtocol, UIScrollViewDelegate { ... }
class MyScrollViewSubclass: UIScrollView {
...
weak var delegate: MyScrollViewSubclassDelegate?
...
}
Run Code Online (Sandbox Code Playgroud)
我使用'MyScrollViewSubclassDelegate'类型获取Property'委托' 不能覆盖类型为'UIScrollViewDelegate?'的属性 .
如何重写视觉格式
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[label]-|", options: .AlignAllBaseline, metrics: nil, views: ["label": label]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label]-|", options: .AlignAllCenterX, metrics: nil, views: ["label": label]))
Run Code Online (Sandbox Code Playgroud)
通过移动到布局指南(带边距)?
我试过了
label.topAnchor.constraintEqualToAnchor(layoutMarginsGuide.topAnchor).active = true
label.leftAnchor.constraintEqualToAnchor(layoutMarginsGuide.leftAnchor).active = true
label.bottomAnchor.constraintEqualToAnchor(layoutMarginsGuide.bottomAnchor).active = true
label.rightAnchor.constraintEqualToAnchor(layoutMarginsGuide.rightAnchor).active = true
Run Code Online (Sandbox Code Playgroud)
但不起作用.甚至layoutMarginsGuide.layoutFrame
没有预期的价值(是的,我在执行layoutSubviews
后称之为super
).设置了约束,但行为就像零保证金一样.layoutFrame
只有当布局边距设置为负时,才会布局并给出预期; 这显然不是我想要的,但表明约束是用边距指南设置的.看起来我错过了一些东西......
我看到没有理由让运算符隐式返回unwrapped可选项.但是它将隐式解包的可选默认值包装到可选(包装)中的重点是什么?我只是期望非可选的结果.我在这里想错了吗?
var defaultValue: Int! = 0
var optional: Int?
let result = optional ?? defaultValue
print(defaultValue.dynamicType) // ImplicitlyUnwrappedOptional<Int>
print(result.dynamicType) // Optional<Int>
Run Code Online (Sandbox Code Playgroud)