【问题标题】:Swift 2 to Swift 2.3 guard let and for errorsSwift 2 到 Swift 2.3 保护让和错误
【发布时间】:2016-10-24 14:56:51
【问题描述】:

我有一个正在工作的项目,但是当打开它时,它给了我 3 个简单的错误。这是我的代码:

1

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

和错误

'let' 作为参数属性是不允许的,需要 ',' 连接多子句条件的部分,

2

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)
      }
    }

和错误

预期的 ',' 连接多子句条件的部分(其中)

3

    func removeAllSubViews(){
        for (var i = 0 ; i < subviews.count ; i++){
            subviews[i].removeFromSuperview()
        }
    }
    
    func loadViewFromNib(nibName:String) -> UIView {
        
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let v = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        
        return v
    }
    
}

错误

'.dynamicType' 已弃用。改用'type(of: ...)'

我尝试了所有方法,但无法解决此问题。

【问题讨论】:

  • 使用, 代替where。而不是self.dynamicType 使用type(of:)

标签: ios swift for-loop swift2


【解决方案1】:

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() }

【讨论】:

    【解决方案2】:

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

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

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

    1. 预期的 ',' 连接多子句条件的各个部分( where )

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

    1. '.dynamicType' 已弃用。改用'type(of: ...)'

    方法签名.dynamicType 已更改为type(of: )。所以更新你的代码以使用type(of:) 而不是dynamicType

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      相关资源
      最近更新 更多