简体   繁体   English

swift无法将类型(_,_)-> _的值转换为预期的参数类型'((_,CGFloat))-> _

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

I am using a library URLEmbeddedView 我正在使用库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. 无法在.flapMap行将(_,_)-> _类型的值转换为期望的参数类型'((_,CGFloat))-> _“。

I am familiar with Objective-C but not with Swift . 我熟悉Objective-C但不熟悉Swift What will be the issue in the code? 代码中的问题是什么?

在此处输入图片说明

Wrong addConstraints() is being called. 错误的addConstraints()被调用。

You're calling viewController.addConstraints() when it appears you want to call view.addConstraints() 您要在调用viewController.addConstraints()时要调用view.addConstraints()

Change the second addConstraints() to view.addConstraints() 将第二个addConstraints()更改为view.addConstraints()

It was also necessary to use Xcode version above 8.1. 还必须使用8.1以上的Xcode版本。

I have used the same code as yours. 我使用了与您相同的代码。 Just applied the constraints to view and it works perfectly. 只需将约束应用于view正常运行。

   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. flatMap语法没有问题。 I have executed the code on Xcode-9 as well as Xcode-8 and it doesn't give me any error. 我已经在Xcode-9Xcode-8上执行了代码,但没有给我任何错误。

Just used the same code that I have written above and see if it compiles correctly. 只是使用了我上面编写的相同代码,然后看它是否可以正确编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 按错误排序:无法转换“CGFloat”类型的值? 到预期的参数类型'Self' - Swift sorted by error: Cannot convert value of type 'CGFloat?' to expected argument type 'Self' 无法将'Float'类型的值转换为预期参数'CGFloat' - Cannot convert value of type 'Float' to expected argument 'CGFloat' Swift 1.2到Swift 2:无法将类型的值转换为预期的参数类型 - Swift 1.2 to swift 2: Cannot convert value of type to expected argument type ios无法将类型“()”的值转换为预期的参数类型“字符串”,迅速3 - ios Cannot convert value of type '()' to expected argument type 'String' swift 3 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift Swift 3.0无法将(_,_)->()类型的值转换为期望的参数类型'ObjectsOrErrorBlock' - Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock' swift:无法将类型“()”的值转换为预期的参数类型“ [Double]” - swift : Cannot convert value of type '()' to expected argument type '[Double]' Swift 无法将类型“()”的值转换为预期的参数类型“() -> Void” - Swift Cannot convert value of type '()' to expected argument type '() -> Void' Swift类别:“无法将类型的值转换为预期的参数类型'AnyObject!' - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM