ios - 调用中的额外参数

标签 ios swift

我正在努力解决 Swift 的这一部分,在该方法调用中我没有看到额外的参数。

struct RectPadding {
    var top: CGFloat
    var right: CGFloat
    var bottom: CGFloat
    var left: CGFloat

    init() {
        top = 0
        right = 0
        bottom = 0
        left = 0
    }

    func setPadding(each: CGFloat) {
        setPadding(top: each, right: each, bottom: each, left: each) // I get the error here
    }
    mutating func setPadding(#top: CGFloat, right: CGFloat, bottom: CGFloat, left: CGFloat) {
        self.top = top
        self.right = right
        self.bottom = bottom
        self.left = left
    }
}

我错过了什么?

最佳答案

这个方法:

mutating func setPadding(each: CGFloat) {
    setPadding(top: each, right: each, bottom: each, left: each) // I get the error here
}

必须声明为mutating,因为它反过来会调用一个mutating方法。

出现错误消息(额外参数)的原因是它尝试调用自身(具有相同的名称),而它只接受一个参数。

关于ios - 调用中的额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767870/

相关文章:

swift - 为什么 String.append 不适用于像 "foo\(bar)"这样的格式化字符串?

ios - 如何在 Swift 3 的子类中覆盖 Init()

swift - 自动完成搜索栏 - 不是值单元格

ios - Swift:在编译时将枚举值映射到类型

ios - 动态对象可以是 UIImageView

iphone - 如何在 iPhone 中获取 UILabel 标签

ios - MPMovieplayer 现在播放 iOS 问题

ios - 更改 rootViewController 不会显示设置为 root 的 viewController

IOS - 线程在 Web 服务调用期间停止

ios - 向 tableView header 添加手势识别器不起作用?