小编Sta*_*ida的帖子

在Swift中委托不同类型的属性

好的,我们有UIScrollView声明:

protocol UIScrollViewDelegate: NSObjectProtocol { ... }
class UIScrollView: UIView {
    ...
    weak var delegate: UIScrollViewDelegate?
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后UITableViewdelegate变种?

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?'的属性 .

swift

7
推荐指数
1
解决办法
1060
查看次数

自动布局:layoutMarginsGuide

如何重写视觉格式

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只有当布局边距设置为负时,才会布局并给出预期; 这显然不是我想要的,但表明约束是用边距指南设置的.看起来我错过了一些东西......

ios autolayout

7
推荐指数
2
解决办法
3378
查看次数

为什么nil合并运算符包含一个隐式解包的默认值?

我看到没有理由让运算符隐式返回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)

swift

3
推荐指数
1
解决办法
532
查看次数

标签 统计

swift ×2

autolayout ×1

ios ×1