Search code examples
iosswiftnslayoutconstraint

swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _


I am using a library URLEmbeddedView

It defines following code in its library:

func addConstraints(with view: UIView, center: CGPoint, multiplier: CGFloat = 1) {
    view.translatesAutoresizingMaskIntoConstraints = false
    addConstraints([
            (.centerX, center.x),
            (.centerY, center.y)
        ]
        .flatMap {
            .init(item: view,
                  attribute: $0,
                  relatedBy: .equal,
                  toItem: self,
                  attribute: $0,
                  multiplier: multiplier,
                  constant: $1)
        })
}

We are generating preview of link through the Library but following error is occurring :

cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _" at .flapMap line.

I am familiar with Objective-C but not with Swift. What will be the issue in the code?

enter image description here


Solution

  • It was also necessary to use Xcode version above 8.1.

    I have used the same code as yours. Just applied the constraints to view and it works perfectly.

       view.addConstraints([
            (.centerX, center.x),
            (.centerY, center.y)
            ].flatMap {
                .init(item: view, attribute: $0, relatedBy: .equal, toItem: self, attribute: $0, multiplier: multiplier, constant: $1)
        })
    

    There is no issue in the flatMap syntax. I have executed the code on Xcode-9 as well as Xcode-8 and it doesn't give me any error.

    Just used the same code that I have written above and see if it compiles correctly.