简体   繁体   中英

Converting a file to Swift 3: unable to infer closure type in the current context

I'm converting some library code in an app of mine, and I can't figure out how to convert this file from Swift 2.3 to Swift 3

import UIKit

struct Constraint{
    var identifier: String?

    var attribute: NSLayoutAttribute = .centerX
    var secondAttribute: NSLayoutAttribute = .notAnAttribute
    var constant: CGFloat = 0
    var multiplier: CGFloat = 1
    var relation: NSLayoutRelation = .equal
}

func attributes(attrs:NSLayoutAttribute...) -> [NSLayoutAttribute]{
    return attrs
}

infix operator >>- : MultiplicationPrecedence

func >>- <T: UIView> (lhs: (T,T), apply: (inout Constraint) -> () ) -> NSLayoutConstraint {
    var const = Constraint()
    apply(&const)

    const.secondAttribute = .notAnAttribute == const.secondAttribute ? const.attribute : const.secondAttribute

    let constraint = NSLayoutConstraint(item: lhs.0,
                                        attribute: const.attribute,
                                        relatedBy: const.relation,
                                        toItem: lhs.1,
                                        attribute: const.secondAttribute,
                                        multiplier: const.multiplier,
                                        constant: const.constant)

    constraint.identifier = const.identifier

    NSLayoutConstraint.activate([constraint])
    return constraint
}

func >>- <T: UIView> (lhs: T, apply: (inout Constraint) -> () ) -> NSLayoutConstraint {
    var const = Constraint()
    apply(&const)

    let constraint = NSLayoutConstraint(item: lhs,
                                        attribute: const.attribute,
                                        relatedBy: const.relation,
                                        toItem: nil,
                                        attribute: const.attribute,
                                        multiplier: const.multiplier,
                                        constant: const.constant)
    constraint.identifier = const.identifier

    NSLayoutConstraint.activate([constraint])
    return constraint
}

func >>- <T:UIView> (lhs: (T,T),attributes: [NSLayoutAttribute]){
    for attribute in attributes{
        lhs >>- {
            $0.attribute = attribute
        }
    }
}

func >>- <T:UIView> (lhs: T, attributes: [NSLayoutAttribute]){
    for attribute in attributes{
        lhs >>- {
            $0.attribute = attribute
        }
    }
}

The error is in the last two functions.

lhs >>- {
    $0.attribute = attribute
}

Here is where it states "unable to infer closure type in the current context"

I managed to solve the issue by explicitly giving a closure the type that the function wants.

func >>- <T:UIView> (lhs: (T,T), attributes: [NSLayoutAttribute]){
    for attribute in attributes{

        let closure: (inout Constraint) -> () = {
            $0.attribute = attribute
        }

        lhs >>- closure
    }
}

I'm not sure why it requires me to explicitly state that, but that ends up working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM