David Seek David Seek - 11 months ago 96
iOS Question

Swift 3 / How to use result of warning "Result of call is unused"

Before marking as duplicate: I have researched and found plenty of threads to this topic, but all of them solve it by silencing the warning with

_ =
. I don't want to silence the warning. I want to use the result!

I'm trying to convert a forked Framework to Swift 3 and am stuck at the warning:

Result of call is unused

At the following code:

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

is a function of that Framework that returns an Array of

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

let constraint = NSLayoutConstraint(
item: self,
attribute: attribute,
relatedBy: .equal,
toItem: self.superview,
attribute: attribute,
multiplier: 1,
constant: constant

return constraints

I am able to silence the warning, but since the result will not get used, the
does not appear in the wanted appearance.

Pic image:

enter image description here

Use it:

enter image description here

with incorrect appearance.

enter image description here

How do I use the result? Help is very appreciated.

Answer Source

You might say:

let constraints = imageView.autoLayoutToSuperview()