简体   繁体   English

Swift 3通用中缀运算符错误

[英]Swift 3 Generic infix operator error

In the process of updating a project from Xcode 7 to 8, I'm facing a problem. 在将项目从Xcode 7更新到8的过程中,我遇到了一个问题。

There is a generic infix operator which handles constraints on UIView s. 有一个通用的infix运算符可处理UIView的约束。

Here is the operator's definition: 这是运算符的定义:

precedencegroup constPrecedence {
  associativity: left
  higherThan: AssignmentPrecedence
}

infix operator >>>- : constPrecedence

@discardableResult
func >>>- <T: UIView> (left: (T, T), block: (inout ConstraintInfo) -> ()) -> NSLayoutConstraint {
  var info = ConstraintInfo()
  block(&info)
  info.secondAttribute = info.secondAttribute == .notAnAttribute ? info.attribute : info.secondAttribute

  let constraint = NSLayoutConstraint(item: left.1,
                                      attribute: info.attribute,
                                      relatedBy: info.relation,
                                      toItem: left.0,
                                      attribute: info.secondAttribute,
                                      multiplier: 1,
                                      constant: info.constant)
  constraint.identifier = info.identifier
  left.0.addConstraint(constraint)
  return constraint
}

now, upon using the operator, I'm getting an error I don't understand: 现在,在使用运算符时,出现了一个我不明白的错误:

   for attribute: NSLayoutAttribute in [.left, .right, .top, .bottom] {
      (view, self) >>>- {
        $0.attribute = attribute
      }
    }

中缀运算符错误

I have tested with non-generic function too, it will still complain about the block's type. 我也使用非泛型函数进行了测试,它仍然会抱怨该块的类型。

Any idea? 任何想法?

PS: I'm not the original author of the code, I'm trying to updated the code for a PR and to change the syntax will impact too much code. PS:我不是代码的原始作者,我正在尝试为PR更新代码,并且更改语法会影响太多代码。

I was able to solve this problem by adding a return call at the end of the code block passed to the operator. 通过在传递给操作员的代码块的末尾添加一个return调用,我能够解决此问题。 It seems like Swift 3.0 compiler cannot infer code block to be a closure without return . 看来Swift 3.0编译器无法推断出代码块是没有return的闭包。 As mentioned in the question's comments, the original operator should work fine with a new Swift 3.0 project, but converting the project from older Swift versions seems to break the operator somehow. 正如问题注释中提到的那样,原始运算符应该可以在新的Swift 3.0项目中正常工作,但是从较早的Swift版本转换项目似乎会以某种方式破坏运算符。

Any way This is the correct usage of the operator now and it works fine: 无论如何,这是现在运算符的正确用法,并且可以正常工作:

 for attribute: NSLayoutAttribute in [.left, .right, .top, .bottom] {
      (view, self) >>>- {
        $0.attribute = attribute
        return
      }
    }

PS: I have been doing this fix before but I was getting a weird Segmentation Fault error 11 after a few of errors' been fixed and I wasn't aware of the reason. PS:我之前一直在进行此修复,但是在修复了一些错误并且我不知道原因之后,我收到了奇怪的Segmentation Fault error 11 Turns out, Xcode could not handle some of the closure types and segmentation fault was happening. 事实证明,Xcode无法处理某些闭包类型,并且发生了分段错误。 Adding return call to ALL places where the operator was being used solved the problem. return叫添加到使用操作员的所有位置可以解决此问题。

I would be happy to discuss the reason and a better solution with any interested developer. 我很乐意与任何感兴趣的开发人员讨论原因和更好的解决方案。

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

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