从 Objective-C 转换后,我在 Swift 中遇到问题。谁能帮我解决这个问题?
var panels = NSArray?
func buttonTotalWidth() -> CGFloat {
var width: CGFloat = 0
for panel: UIView? in panels {
width += panel?.frame.width
}
return width
}
我的 objective-c 代码
@property NSArray *panels;
- (CGFloat)buttonTotalWidth {
CGFloat width = 0;
for (UIView *panel in self.panels) {
width += CGRectGetWidth(panel.frame);
}
return width;
}
最佳答案
请不要尝试将 ObjC 代码逐字翻译成 Swift。学习 Swift 特定的东西。
一个 swift 的翻译可能是
var panels = [UIView]()
func buttonTotalWidth() -> CGFloat {
return panels.map{$0.frame.width}.reduce(0.0, + )
}
关于swift - 类型 'NSArray?.Type' 不符合协议(protocol) 'Sequence',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123588/