Swift 中的重命名问题 [英] Renamed issues in Swift

查看:35
本文介绍了Swift 中的重命名问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用 Swift 进行编程,在这样做的过程中,我正在关注 教程.不幸的是,本教程看起来有点过时,而且大部分代码都在抛出构建时错误.最经常出现的错误是 NSURLSession 已重命名为 URLSession.我试过让 Swift 修复它,但在很多情况下它只是开始抛出警告.我还收到一个 Value type HomeModel has no member'parseJSON' 错误以及一个 NSDat不能隐式转换为 data 错误.据我所知,似乎不再使用 NSURL,但我不确定其他两个.看到这是我从事的第一个 Swift 项目,我不知道如何解决这些问题.有人可以提供一些有关如何解决这些错误的见解吗?

I'm working on programming with Swift for the first time, and in doing so I'm following along with this tutorial. Unfortunately it looks like the tutorial is a little outdated and most of the code is throwing Buildtime errors. The most reoccurring error is the NSURLSession has been renamed to URLSession. I've tried letting Swift fix it, but in many cases it just starts throwing warnings.I'm also getting a Value type HomeModel has no member'parseJSON' error as well as a NSDat is not implicitly convertible to data error. From what I can tell, it looks like the NSURL is no longer used, but I'm not sure about the other two. Seeing how this is the first Swift project I've worked on, I'm not sure how to fix these. Can someone provide some insight on how to fix these mistakes?

代码如下:

import Foundation

protocol HomeModelProtocal: class {
    func itemsDownloaded(items: NSArray)
}


class HomeModel: NSObject, NSURLSessionDataDelegate {

    //properties

    weak var delegate: HomeModelProtocal!

    var data : NSMutableData = NSMutableData()

    let urlPath: String = "http://testurl.com/service.php" //this will be changed to the path where service.php lives


    func downloadItems() {

        let url: NSURL = NSURL(string: urlPath)!
        var session: NSURLSession!
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()


        session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)

        let task = session.dataTaskWithURL(url)

        task.resume()

    }

    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
        self.data.appendData(data);

    }

    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
        if error != nil {
            print("Failed to download data")
        }else {
            print("Data downloaded")
            self.parseJSON()
        }

    }
}

推荐答案

几个基本类型在 Swift 3.0 中去掉了NS"前缀.在 swift 2.2 的早期,我们曾经有 NSUserDefaultsNSURLSessionNSFileManager 等.现在,他们中的大多数都去掉了前缀NS"并改变了UserDefaultsURLSessionFileManager

Several basic types have dropped the "NS" prefix in Swift 3.0. Earlier in swift 2.2, we used to have NSUserDefaults, NSURLSession, NSFileManager etc. Now, most of them dropped their prefix "NS" and changed to UserDefaults, URLSession, FileManager etc.

您的代码包含许多带有NS"前缀的类型.只需删除它,您的代码就可以转换为 Swift 3.您转换后的代码如下所示:

Your code contains a lot of types with 'NS' prefix. By simply removing it, your code can be converted to Swift 3. Your converted code looks like as shown below:

protocol HomeModelProtocal: class {
   func itemsDownloaded(items: NSArray)
}

class HomeModel: NSObject, URLSessionDataDelegate {

   //properties

   weak var delegate: HomeModelProtocal!

   var data : Data = Data()

   let urlPath: String = "http://testurl.com/service.php" //this will be changed to the path where service.php lives


   func downloadItems() {

      let url: URL = URL(string: urlPath)!
      var session: URLSession!
      let configuration = URLSessionConfiguration.default

      session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

      let task = session.dataTask(with: url)

      task.resume()
   }

   func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
       self.data.append(data);
   }

   func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
       if error != nil {
          print("Failed to download data")
       }else {
          print("Data downloaded")
          self.parseJSON() // This class doesn't have a function parseJSON(). So, it's giving you an error like this
       }
   }
}

此外,我在您的课程中没有看到任何名为 parseJSON() 的函数.我相信你必须添加它.

Also, I don't see any function called parseJSON() in your class. I believe you have to add it.

这篇关于Swift 中的重命名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆