Swift 2到Swift 2.3后卫和错误

时间:2016-10-24 14:56:51

标签: ios swift for-loop swift2.3

您好我有工作项目但是在打开时它给了我简单的3个错误。我的代码在下面

1

public class WaitableContainer<T> {
  private T object;
  public <T> peekAwait() {...} // returns object if object !=null or makes the caller thread sleep until object != null
  public actuate(T object) {
    this.object = object; // here all sleeping threads must awake and receive the new object
  }
}

和错误

  

&#39;让&#39;作为参数属性是不允许的,预期&#39;,&#39;加入多条款条件的部分,

2

   let location, let response, let error) in

        guard let _:NSURL = location, let _:NSURLResponse = response  where error == nil else {
            print("error")
            return
        }

和错误

  

预期&#39;,&#39;连接多子句条件的部分(其中)

3

for constraint in self.contentView.constraints {
      if let item = constraint.firstItem as? UIView where item == containerView {
        let newConstraint = NSLayoutConstraint( item: anAnimationView, attribute: constraint.firstAttribute,
          relatedBy: constraint.relation, toItem: constraint.secondItem, attribute: constraint.secondAttribute,
          multiplier: constraint.multiplier, constant: constraint.constant)

        newConstraints.append(newConstraint)
      } else if let item: UIView = constraint.secondItem as? UIView where item == containerView {
        let newConstraint = NSLayoutConstraint(item: constraint.firstItem, attribute: constraint.firstAttribute,
          relatedBy: constraint.relation, toItem: anAnimationView, attribute: constraint.secondAttribute,
          multiplier: constraint.multiplier, constant: constraint.constant)

        newConstraints.append(newConstraint)
      }
    }

错误

  

&#39; .dynamicType&#39;已弃用。使用&#39;类型(of:...)&#39;代替

我尝试了一切,但我没有解决这个问题。 TY

2 个答案:

答案 0 :(得分:3)

1)

(location, response, error) in
    guard error == nil else {
        print(error!)
        return
    }
// In this scope `location` and `response` are optional but guaranteed not `nil`

(location, response, error) in
    guard let loc = location, let resp = response, error == nil else {
        print(error!)
        return
    }

2)

if let item = constraint.firstItem as? UIView, item == containerView {

3)

let bundle = NSBundle(forClass: type(of:self))

4 removeAllSubViews的Swift方式是(没有丑陋的C风格循环)

func removeAllSubViews() { subViews.forEach{ $0.removeFromSuperview() }

答案 1 :(得分:1)

在Swift 3中,发生了很多变化。

  
      
  1. 'let'作为参数属性是不允许的,预期','连接多子句条件的部分,
  2.   

您在guardif let语句中执行的每项操作都应以适当的letvar作为前缀,并且应以逗号分隔。

  
      
  1. 预期','加入多条款条件的部分(在哪里)
  2.   

where子句不能再用于guard语句中。您只需删除where并将其替换为,分隔符即可。

  
      
  1. '。dynamicType'已弃用。使用'type(of:...)'代替
  2.   

方法签名.dynamicType已更改为type(of: )。因此,请更新代码以使用type(of:)代替dynamicType