swifturlsessionnsurlsessiondatatask

URLSession.dataTask return the data with 0


I have a web page for giving me the server data in JSON format and I am trying to to reach the site and get these objects. But the data will return with 0; empty variable. My code like this.

    func downloadItems() {

    let url: URL = URL(string: urlPath)!

    let defaultSession = URLSession(configuration: URLSessionConfiguration.default)

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

        if error != nil {
            print("Failed to download data")
        }else {
            let httpResponse = response as? HTTPURLResponse
            print("data is like: \(data as! NSData)")
            print(httpResponse)
            self.data = data!
            print("Data downloaded")
            self.parseJSON(self.data)
        }

    }

    task.resume()
}

func parseJSON(_ data: Data){
    var jsonResult = NSArray()

    do{
        jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as! NSArray
        print(jsonResult)
    } catch let error as NSError {
        print(error)
    }

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

    for tree in 0 ..< jsonResult.count {

        jsonElement = jsonResult[tree] as! NSDictionary

        let tempTree = trees()

        if let latinName = jsonElement["latin_name"] as? String,
        let turkishName = jsonElement["turkish_name"] as? String,
        let seedType = jsonElement["seed_type"] as? Int,
        let leafType = jsonElement["leaf_type"] as? Int,
        let spreadingArea = jsonElement["spreading_area"] as? String,
        let bothanicalProp = jsonElement["bothanical_prop"] as? String {
            tempTree.latin_name = latinName
            tempTree.turkish_name = turkishName
            tempTree.seed_type = seedType
            tempTree.leaf_type = leafType
            tempTree.spreading_area = spreadingArea
            tempTree.botanical_prop = bothanicalProp
        }

        forest.add(tempTree)

    }

    DispatchQueue.main.async {
        self.delegate.itemsDownloaded(items: forest)
    }

}

and my main view controller is like that

    override func viewDidLoad() {
    super.viewDidLoad()

    //table view delegats and datasource declaration
    treeTableView.delegate = self
    treeTableView.dataSource = self

    let treeModel = trees()
    treeModel.delegate = self
    treeModel.downloadItems(completion: <#T##(Data?) -> Void#>)

}

func itemsDownloaded(items: NSArray) {
    feedTrees = items
    self.treeTableView.reloadData()
}

When I enter the site the JSON object is here. But when I tried to retrieve the object with dataTask. Data will be 0. Also HTTP Response is 200. Is there any knowledge about this situation ? Thanks and good coding.

print(jsonResult) is

{
    "bothanical_prop" = "T\U00fcrkiyede park ve bah\U00e7elerde dekoratif ama\U00e7la kullan\U0131l\U0131r. Do\U011fal yay\U0131l\U0131\U015f alan\U0131 \U015eili'nin g\U00fcneyi ve G\U00fcnaybat\U0131 Arjantin'deki And Da\U011flar\U0131d\U0131r.";
    "latin_name" = "Araucaria araucana (Molina) K. Koch.";
    "leaf_type" = 1;
    "seed_type" = 1;
    "spreading_area" = "Do\U011fal alanlarda erkekler 15-18 m, di\U015filer ise 30-50 m ye kadar boylanabilir. Park ve bah\U00e7elerde ise 10 m'yi nadiren ge\U00e7en herdemye\U015fil a\U011fa\U00e7lard\U0131r. G\U00f6zvdesi d\U00fczg\U00fcnd\U00fcr; \U00f6zellikle gen\U00e7 bireyleri simetriktir ve konik formludur. Yapraklar\U0131, deri gibi sert, u\U0308\U00e7gen \U015feklinde, u\U00e7lar\U0131 sivri, bat\U0131c\U0131 ve her iki yu\U0308zu\U0308 koyu ye\U015fildir.";
    "turkis_name" = "Maynum \U00c7\U0131kmaz A\U011fac\U0131";
},
    {
    "bothanical_prop" = "Norfolk adalar\U0131na (Avustralya) \U00f6zgu\U0308 endemik bir tu\U0308rdu\U0308r. Dekoratif ama\U00e7l\U0131 \U00fclkemizde Ege, Akdeniz ve Marmara \U00e7evresinde d\U0131\U00e7 mekan bitkisi olarak kullan\U0131l\U0131r.";
    "latin_name" = "Araucaria heterophylla (Salisb.) Franco";
    "leaf_type" = 1;
    "seed_type" = 1;
    "spreading_area" = "60-70 m\U2019ye kadar boylanabilen bir orman a\U011fac\U0131d\U0131r. Dallar g\U00f6vdeden 4-7\U2019li \U00e7evrel olarak \U00e7\U0131kar. G\U00f6vde kabu\U011fu ince levhalar halinde \U00e7atlakl\U0131d\U0131r. Yapraklar\U0131 2 farkl\U0131 \U015fekildedir: Gen\U00e7 ve yan su\U0308rgu\U0308nlerde a\U00e7\U0131k ye\U015fil, biz gibi uzun, k\U00f6\U015feli ve yumu\U015fak; ya\U015fl\U0131 su\U0308rgu\U0308nlerde ise daha k\U0131sa, daha sert, s\U0131k, birbiri u\U0308zerine binmi\U015f ve u\U00e7 k\U0131s\U0131mlar\U0131 boynuz gibi \U00f6ne do\U011fru k\U0131vr\U0131lm\U0131\U015f olan yapraklar vard\U0131r";
    "turkis_name" = "Salon arokaryas\U0131";
}

Solution

  • Seems your API returns turkis_name instead of turkish_name:

    "turkis_name" = "Maynum \U00c7\U0131kmaz A\U011fac\U0131";

    Tell your server side engineers fix it to follow the API spec. Or, is that you who mis-read the API spec?


    Anyway, when you need to adapt your code to the actual response, you may need to fix only one line.

        let turkishName = jsonElement["turkis_name"] as? String,
    

    But I give you a practically useful advice:

    Do not ignore errors or invalid inputs silently.

    With some modifications to make your code more Swifty, your parseJSON(_:) would be something like below:

    func parseJSON(_ data: Data) -> NSArray {
        do{
            guard let jsonResult = try JSONSerialization.jsonObject(with: data) as? [[String: Any]] else {
                print("Invalid JSON structure")
                return []
            }
    
            let forest = NSMutableArray()
    
            for jsonElement in jsonResult {
                if
                    let latinName = jsonElement["latin_name"] as? String,
                    let turkishName = jsonElement["turkis_name"] as? String, //###Use the right key here
                    let seedType = jsonElement["seed_type"] as? Int,
                    let leafType = jsonElement["leaf_type"] as? Int,
                    let spreadingArea = jsonElement["spreading_area"] as? String,
                    let bothanicalProp = jsonElement["bothanical_prop"] as? String
                {
                    let tempTree = trees()
    
                    tempTree.latin_name = latinName
                    tempTree.turkish_name = turkishName
                    tempTree.seed_type = seedType
                    tempTree.leaf_type = leafType
                    tempTree.spreading_area = spreadingArea
                    tempTree.botanical_prop = bothanicalProp
    
                    forest.add(tempTree)
                } else {
                    //### Do not ignore errors or invalid inputs silently.
                    print("Missing any of 'latin_name', 'turkis_name', 'seed_type', 'leaf_type', 'spreading_area', 'bothanical_prop'", jsonElement)
                }
            }
            return forest
    
        } catch let error {
            print(error)
            return []
        }
    }
    

    (There are some more parts I want to fix, but those needs modification of the hidden parts of your code, so I have given up. And consider adopting Codable, when working with JSON.)


    You may need some more fixes, but when you write something about unexpected result, please remember to include:

    Putting print statement and showing the output with the code including it would be a good way.