David Seek David Seek - 1 month ago 18
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() {
contentView.addSubview(imageView)
imageView.autoLayoutToSuperview() // here, result of autoLayoutToSuperview() is unused
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.backgroundColor = UIColor.white
}


autoLayouttoSuperview
is a function of that Framework that returns an Array of
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
}


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

Pic image:

enter image description here

Use it:

enter image description here

imageView
with incorrect appearance.

enter image description here

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

Answer

You might say:

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