【问题标题】:Swift 3 / How to use result of warning "Result of call is unused"Swift 3 / 如何使用警告结果“调用结果未使用”
【发布时间】:2017-03-02 20:18:56
【问题描述】:

在标记为重复之前:我已经研究并发现了很多关于这个主题的线程,但他们都通过使用_ = 消除警告来解决这个问题。我不想让警告静音。我要使用结果!

我正在尝试将分叉的框架转换为 Swift 3,但遇到了警告:

调用结果未使用

如下代码:

func setupViews() {
    contentView.addSubview(imageView)
    imageView.autoLayoutToSuperview()   // here, result of autoLayoutToSuperview() is unused
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    imageView.backgroundColor = UIColor.white
}

autoLayouttoSuperview 是该框架的一个函数,它返回一个 NSLayoutConstraint 数组。

func autoLayoutToSuperview(_ attributes: [NSLayoutAttribute] = [.left, .right, .top, .bottom], inset: CGFloat = 0) -> [NSLayoutConstraint] {

    var constraints: [NSLayoutConstraint] = []
    translatesAutoresizingMaskIntoConstraints = false

    for attribute in attributes {

        var constant = inset
        switch attribute {
        case .right:
            constant = -inset
        case .bottom:
            constant = -inset
        default:
            break
        }

        let constraint = NSLayoutConstraint(
            item: self,
            attribute: attribute,
            relatedBy: .equal,
            toItem: self.superview,
            attribute: attribute,
            multiplier: 1,
            constant: constant
        )
        self.superview?.addConstraint(constraint)
        constraints.append(constraint)
    }

    return constraints
}

我可以使警告静音,但由于结果不会被使用,imageView 不会出现在想要的外观中。

图片图片:

使用它:

imageView 外观不正确。

如何使用结果?非常感谢您的帮助。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    你可能会说:

    let constraints = imageView.autoLayoutToSuperview()
    NSLayoutConstraint.activate(constraints)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 2012-09-02
      • 2019-09-26
      相关资源
      最近更新 更多