gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:25:28 25 4
gpt4 key购买 nike

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

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com