ios - Swift 3/如何使用警告结果 "Result of call is unused"

标签 ios swift

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

我正在尝试将 fork 的框架转换为 Swift 3,但卡在警告处:

Result of call is unused

在以下代码处:

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 不会出现在想要的外观中。

图片:

enter image description here

使用它:

enter image description here

imageView 外观不正确。

enter image description here

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

最佳答案

你可能会说:

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

关于ios - Swift 3/如何使用警告结果 "Result of call is unused",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40161084/

相关文章:

ios - 在 UITableView 中分页数据

ios - tableview 不读取标签

ios - 如果我想在之后为自动布局约束设置动画,我该如何修改我的 UIView 扩展?

ios - 构建平台之一时出错 : IOS

ios - 模拟中的按钮大小与 Storyboard 中的按钮大小有很大不同

javascript - 如何在 iOS 中禁用单个 HTML 元素的触摸操作

swift - 使用 Swift 解析 JSON 错误

swift ,macOS : Open terminal window at the location

ios - 使用 Swift 制作可点击的 UILabel

ios - 更新 Xcode 后 pod 文件出现错误,如何解决?