【问题标题】:iOS 9 errors and correct conversion to swift 2iOS 9 错误并正确转换为 swift 2
【发布时间】:2015-10-12 18:04:15
【问题描述】:

所以在更新到 iOS 9 和 Swift 2 后,我的项目中出现了很多错误,即使单击自动转换选项也没有得到修复。幸运的是,大多数都很简单,我能够弄清楚它们,但我还剩下三个主要的。有人能帮我解决这些问题吗?

非常感谢!

代码块 #1

override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
    return attributesList
}

块 1 出错

Method does not override any method from its superclass  

代码块 #2

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

块 2 错误

A non-failable initializer cannot chain to failable initializer 'init(coder:)' written with 'init?'

代码块 #3

 NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

块 3 错误

'(NSURLResponse!, NSData!, NSError!) -> Void' is not convertible to '(NSURLResponse?, NSData?, NSError?) -> Void'

编辑:现在第三个是固定的(但仍然不推荐使用)我在下面的代码中得到了这个错误:

代码:

var dictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary

错误:

Extra argument 'error' in call

【问题讨论】:

    标签: swift2 ios9


    【解决方案1】:
    1. layoutAttributesForElementsInRect 现在返回 [UICollectionViewLayoutAttributes]? 而不是 [AnyObject]? 写:

      override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
      }
      
    2. Super.init 现在是错误的 public init?(coder aDecoder: NSCoder) 这意味着它可能返回 nil 所以你必须写:

      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
      }
      
    3. 在这种情况下不要使用显式类型。随便写:

      NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
      
      }
      
    4. 阅读有关错误处理的信息,因为 try! 可能会让您的应用程序失望:/

      try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
      

    sendAsynchronousRequest 在 iOS 9 中已弃用

    【讨论】:

    • 好的,非常感谢!但是现在我在#3 下面有一个错误...我编辑了上面的代码。
    • 再次感谢!但是,现在它不会获取应用程序的解析数据,并显示“2015-10-12 11:48:28.682 FiveTest[2456:98960] [Error]: The resource could not be loaded because the App Transport Security policy requires使用安全连接。(代码:100,版本:1.8.2)“...知道这是什么意思吗?
    • @Echizzle 是的。你必须使用https 而不是http
    • @Echizzle 您需要编辑您的信息列表并添加您的应用需要访问的域
    • @Echizzle 除了那个问题meta.stackexchange.com/questions/5234/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多