dataTask使用NSURL而不是URL

时间:2018-01-07 08:20:20

标签: ios swift

我一直在关注指南,希望学习如何将MYSQL用于IOS应用程序。

然而,指南有点过时,我使用swift 3,我一直在编辑代码来修复一些错误。 我已经归结为最后一个问题,即在我从使用URL更改为NSURL之后,我无法使用" DataTask"再来..

我不知道如何替换这行代码。

import Foundation

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

class HomeModel: NSObject, NSURLSessionDataDelegate {

    //properties

    weak var delegate: HomeModelProtocol!

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

    func downloadItems() {

        let url: NSURL = NSURL(string: urlPath)!
        let defaultSession = Foundation.NSURLSessionConfiguration

        let task = defaultSession.dataTask(with: url) { (data, response, error) in

            if error != nil {
                print("Failed to download data")
            }else {
                print("Data downloaded")
                self.parseJSON(data!)
            }

        }

        task.resume()
    }
}
func parseJSON(_ data:Data) {

    var jsonResult = NSArray()

    do{
        jsonResult = try NSJSONSerialization.jsonObject(with: data, options: NSJSONSerialization.ReadingOptions.allowFragments) as! NSArray

    } catch let error as NSError {
        print(error)

    }

    var jsonElement = NSDictionary()
    let locations = NSMutableArray()

    for i in 0 ..< jsonResult.count
    {

        jsonElement = jsonResult[i] as! NSDictionary

        let location = LocationModel()

        //the following insures none of the JsonElement values are nil through optional binding
        if let name = jsonElement["Name"] as? String,
            let address = jsonElement["Address"] as? String,
            let latitude = jsonElement["Latitude"] as? String,
            let longitude = jsonElement["Longitude"] as? String
        {

            location.name = name
            location.address = address
            location.latitude = latitude
            location.longitude = longitude

        }

        locations.addObject(location)

    }

1 个答案:

答案 0 :(得分:1)

  

我遇到了最后一个问题,即在我从使用URL更改为NSURL之后,我无法使用&#34; DataTask&#34;了..

为什么从URL切换到NSURL?那是在朝着错误的方向前进。 URLNSURL的Swift桥。它应该替换所有新代码中的NSURL

切换回URL。如果您必须使用NSURL,则必须在as URL中使用dataTask(with:)时添加URL,因为该方法需要// vvv Changed NSURLSessionDataDelegate to URLSessionDataDelegate class HomeModel: NSObject, URLSessionDataDelegate { weak var delegate: HomeModelProtocol? // <-- Avoid ! for this let urlPath = "http://iosquiz.com/service.php" func downloadItems() { let url = URL(string: urlPath)! // <-- Changed NSURL to URL let defaultSession = URLSession.shared // <-- Use URLSession, not URLSessionConfiguration let task = defaultSession.dataTask(with: url) { (data, response, error) in if error != nil { print("Failed to download data") }else { print("Data downloaded") self.parseJSON(data!) } } task.resume() } }

这里存在更深层次的问题。您正在使用配置,就好像它是会话一样。这是我认为你的意思的代码:

import numpy as np
import pandas as pd
data = np.array([11234466, 77777777, 12345678, 23452345])
data1 = np.array([99999999, 66666666, 44332211, 56781234])
df=pd.DataFrame({'A' : [data.tolist()]})
df1=pd.DataFrame({'A' : [data1.tolist()]})
df=pd.concat([df,df1])