修改Swift代码的最佳方法是什么?
我是Swift 3/Xcode8的新手,但在这个网站以及互联网上的其他资源的帮助下,我正在慢慢地自学一些细节 这听起来像一个完整的noob问题,但是什么是优化代码的最佳方法呢修改Swift代码的最佳方法是什么?,swift,Swift,我是Swift 3/Xcode8的新手,但在这个网站以及互联网上的其他资源的帮助下,我正在慢慢地自学一些细节 这听起来像一个完整的noob问题,但是什么是优化代码的最佳方法呢 @IBOutlet var initialInvestigationCollection: [UIView]! { didSet { initialInvestigationCollection.forEach { $0.isHidden = true }
@IBOutlet var initialInvestigationCollection: [UIView]! {
didSet {
initialInvestigationCollection.forEach {
$0.isHidden = true
}
}
}
@IBAction func expandInitialInvestigationBtn(_ sender: Any) {
UIView.animate(withDuration: 0.1) {
self.initialInvestigationCollection.forEach {
$0.isHidden = !$0.isHidden
}
}
}
我觉得我想创建一个函数并在每个方法中调用它。唯一的问题是,initialInvestigationCollection
和expandInitialInvestigationBtn
每次都会改变,我打算在类中的10个不同部分上运行这两个函数
也许我只是懒惰,不想重复地把它打印出来 如果您发布链接,将在代码审阅中重新发布,但我会这样做:
@IBOutlet var initialInvestigationCollection: [UIView]! {
didSet {
setInitialInvestigationsHidden(to: true, animated: false)
}
}
@IBAction func expandInitialInvestigationBtn(_ sender: Any) {
toggleInitialInvestigationsHidden()
}
func toggleInitialInvestigationsHidden() {
setInitialInvestigationsHidden(to: initialInvestigationCollection[0].isHidden)
}
func setInitialInvestigationsHidden(to hidden: Bool, animated: Bool = true) {
if animated {
UIView.animate(withDuration: 0.1) {
initialInvestigationCollection.forEach { $0.isHidden = hidden }
}
}
else {
initialInvestigationCollection.forEach { $0.isHidden = hidden }
}
}
它为您提供了显示、隐藏或切换的灵活性,清除了代码其他部分中的用法,但仍然清楚代码的意图。我了解代码的作用。但是您希望代码做什么呢?基本上,
initialInvestigationCollection
将被隐藏,直到用户单击展开按钮,然后展开initialInvestigationCollection
中包含的视图。这样您就做得很好了。我的答案虽然在语法上不同,但本质上是相同的。我追求明确(这就是为什么我问你想要什么)。不过,您拥有的代码是有效的。也许你需要进行代码审查?谢谢,我将尝试codereview。您不能设置isHidden
属性的动画。