Ios tableview是空的,但我看到游乐场中的数据来自urlsession

Ios tableview是空的,但我看到游乐场中的数据来自urlsession,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我是swift的初学者,我使用url会话通过post请求填充我的tableview,我在操场上看到数据,但我发现tableview是空的,我不知道为什么,但我使用的模型是Rent和Rents 租金模式: import Foundation class Rent { var location_id: Int var datelocation:String var adresselocation: String var user_id: Int v

我是swift的初学者,我使用url会话通过post请求填充我的tableview,我在操场上看到数据,但我发现tableview是空的,我不知道为什么,但我使用的模型是Rent和Rents

租金模式:

import Foundation

class Rent {
    
    var location_id: Int
    var datelocation:String
    var adresselocation: String
    var user_id: Int
    var bike_id: Int
    var model: String
    var type: String
    var price: String
    var image: String
    
    init(id: Int ,date: String , adresse: String , user: Int , bike: Int , model: String ,type:String ,price:String ,image:String ){
        self.location_id = id
        self.datelocation = date
        self.adresselocation = adresse
        self.model = model
        self.user_id = user
        self.bike_id = bike
        self.type = type
        self.price = price
        self.image = image
    }
    
}
我还使用了另一个模型:

import Foundation

class Rents: Codable {
    
    var location_id: Int
    var datelocation:String
    var adresselocation: String
    var user_id: Int
    var bike_id: Int
    var model: String
    var type: String
    var price: String
    var image: String 
}
我的urlsession是post查询,但我检索了tableview所需的所有数据

这里是我的tableview代码:

import UIKit
import CoreData

class rentlistViewController: UIViewController , UITableViewDataSource, UITableViewDelegate{

    
    var u = ConnectedUser()
    var rents =  [Rent]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.DisplayConnectedUser()
        /*DispatchQueue.main.async {
            self.getRents()
        }*/
        
        //post
    
        guard let url = URL(string: "http://localhost:3000/locations") else {
        return
        }
        
        let bodyparameters = [ "user_id": self.u.user_id! , "name": self.u.name! , "lastname": self.u.lastname! , "email": self.u.email! , "password": self.u.password! , "phone": self.u.phone! ] as [String : Any]
        
       print(bodyparameters)
        
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: bodyparameters, options: []) else{
            print("error in sending data")
            return
            }
        request.httpBody = httpBody
        let session = URLSession.shared
        session.dataTask(with: request) { (data,response,error) in
            if let response = response {
                print(response)
            }
            
            if let data = data {
                do {
                    //let json = try JSONSerialization.jsonObject(with: data, options: [])
                   // print(json);
                    print(data)
                    let rt = try JSONDecoder().decode([Rents].self, from: data)
                
                        print(rt)
                        
                        for item in rt {
                            let id = item.location_id
                            let adresselocation = item.adresselocation
                            let datelocation = item.datelocation
                            let user = item.user_id
                            let bike = item.bike_id
                            let model = item.model
                            let type = item.type
                            let price = item.price
                            let image = item.image
                            self.rents.append(Rent(id: id ,date: datelocation, adresse: adresselocation,user: user, bike: bike,  model: model , type: type , price: price, image: image))
                        }
                        for item in self.rents {
                            print(item.location_id)
                            print(item.adresselocation)
                            print(item.datelocation)
                            print(item.user_id)
                            print(item.bike_id)
                            print(item.model)
                            print(item.type)
                            print(item.price)
                            print(item.image)
                        }
                        print(self.rents)
                        
                }catch{
                    print("error in parsing")
                    print(error)
                }
                
            }
            
        }.resume()
        
        
    }
    
    func DisplayConnectedUser() {
            
             let appDelegate = UIApplication.shared.delegate as! AppDelegate
                //represente l'ORM
                let persistentContainer = appDelegate.persistentContainer
                
                let managedContext = persistentContainer.viewContext     //retourne NSManagedObject toujours
                
                //la requete retourne un NSManagedObject
                let request = NSFetchRequest<NSManagedObject>(entityName :   "Users")
                
                //execution de la requete
                do {
                
                    let result = try  managedContext.fetch(request)
                for item in result {
                    print(item.value(forKey: "user_id") as! Int )
                    print(item.value(forKey: "email")  as! String)
                    self.u.user_id  = (item.value(forKey: "user_id")  as! Int)
                    self.u.email = (item.value(forKey: "email")  as! String)
                    self.u.password = (item.value(forKey: "password")  as! String)
                    self.u.name = (item.value(forKey: "name")  as! String)
                    self.u.lastname = (item.value(forKey: "lastname")  as! String)
                    self.u.phone = (item.value(forKey: "phone")  as! String)
                   
                    print(self.u.user_id!)
                    print(self.u.email!)
                    print(self.u.password!)
                    print(self.u.name!)
                    print(self.u.lastname!)
                    print(self.u.phone!)
                  
                }
                
                   }
                   catch {
                   print("NO DATA FOUND , Error")
                   }


        }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rents.count
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellRent")
        let contentView = cell?.contentView
        let label = contentView?.viewWithTag(1) as! UILabel
     
        DispatchQueue.main.async {
            label.text = self.rents[indexPath.row].model
            print("heloooooo"+label.text!)
        }
            
        return cell!
    }
  
    
    //passage de parametres entre les controleurs
 
         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let rent = self.rents[indexPath.row]
            performSegue(withIdentifier: "mRentDetails" , sender: rent) //passage de variable locale)
            
        }
        
        /* prepare est pour passer les parametres  */
        override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        
        if segue.identifier == "mRentDetails" {
        
        let rent = sender as! Rent
        let destination = segue.destination as! RentDetailsViewController
            destination.datelocation = rent.datelocation
       // destination.hours = rent
       // destination.totalprice = rent
            destination.bikemodel = rent.model
            destination.biketype = rent.type
            destination.priceperhour = rent.price
        
        }}
 
    
}
导入UIKit
导入CoreData
类rentlistViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
var u=ConnectedUser()
var租金=[租金]()
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
self.DisplayConnectedUser()
/*DispatchQueue.main.async{
self.getRents()
}*/
//职位
guard let url=url(字符串:http://localhost:3000/locations)其他{
返回
}
让bodyparameters=[“user\u id”:self.u.user\u id!,“name”:self.u.name!,“lastname”:self.u.lastname!,“email”:self.u.email!,“password”:self.u.password!,“phone”:self.u.phone!]作为[字符串:任意]
打印(车身参数)
var-request=URLRequest(url:url)
request.httpMethod=“POST”
request.addValue(“应用程序/json”,forHTTPHeaderField:“内容类型”)
guard let httpBody=try?JSONSerialization.data(使用jsonObject:bodyparameters,选项:[])else{
打印(“发送数据时出错”)
返回
}
request.httpBody=httpBody
让session=URLSession.shared
会话中的session.dataTask(带:request){(数据、响应、错误)
如果让响应=响应{
打印(答复)
}
如果let data=data{
做{
//让json=try JSONSerialization.jsonObject(使用:data,选项:[])
//打印(json);
打印(数据)
让rt=try JSONDecoder().decode([Rents].self,from:data)
打印(rt)
对于rt中的项目{
let id=项目位置\u id
设adresselocation=item.adresselocation
让datelocation=item.datelocation
let user=item.user\u id
let bike=item.bike\u id
让model=item.model
let type=item.type
让价格=物品价格
让image=item.image
self.rents.append(租金(id:id,日期:datelocation,address:address-location,user:user,bike:bike,model:model,type:type,price:price,image:image))
}
对于自助租赁中的项目{
打印(项目位置\u id)
打印(项目地址位置)
打印(项目日期位置)
打印(项目.用户\u id)
打印(项目.自行车id)
打印(项目.型号)
打印(项目类型)
打印(项目价格)
打印(项目.图像)
}
打印(自助租赁)
}抓住{
打印(“解析错误”)
打印(错误)
}
}
}1.简历()
}
func DisplayConnectedUser(){
让appDelegate=UIApplication.shared.delegate为!appDelegate
//奥姆代表酒店
让persistentContainer=appDelegate.persistentContainer
让managedContext=persistentContainer.viewContext//retourne NSManagedObject toujours
//重新规划项目
let request=NSFetchRequest(entityName:“用户”)
//重新执行
做{
let result=尝试managedContext.fetch(请求)
对于结果中的项目{
打印(项值(forKey:“用户id”)为!Int)
打印(item.value(forKey:“email”)为!字符串)
self.u.user\u id=(item.value(forKey:“user\u id”)as!Int)
self.u.email=(item.value(forKey:“email”)as!String)
self.u.password=(item.value(forKey:“password”)作为!字符串)
self.u.name=(item.value(forKey:“name”)as!String)
self.u.lastname=(item.value(forKey:“lastname”)as!String)
self.u.phone=(item.value(forKey:“phone”)as!String)
打印(self.u.user\u id!)
打印(self.u.email!)
打印(self.u.password!)
打印(self.u.name!)
打印(self.u.lastname!)
打印(self.u.phone!)
}
}
抓住{
打印(“未找到数据,错误”)
}
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回租金。计数
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符为“cellRent”)
让contentView=单元格?.contentView
让label=contentView?.viewWithTag(1)作为!UILabel
D
["email": "faresbenslama95@gmail.com", "password": "123456", "user_id": 10, "phone": "12345671", "name": "benslama", "lastname": "fares"]
<NSHTTPURLResponse: 0x600003848120> { URL: http://localhost:3000/locations } { Status Code: 200, Headers {
    Connection =     (
        "keep-alive"
    );
    "Content-Length" =     (
        339
    );
    "Content-Type" =     (
        "text/html; charset=utf-8"
    );
    Date =     (
        "Mon, 14 Dec 2020 15:50:11 GMT"
    );
    Etag =     (
        "W/\"153-s80IUuqdLC0Rkz3oIqCM7x8xP7I\""
    );
    "Keep-Alive" =     (
        "timeout=5"
    );
    "X-Powered-By" =     (
        Express
    );
} }
339 bytes
[Bicycall.Rents, Bicycall.Rents]
3
Klibia
15/10/2020 15:11
10
3
Skygrey
RTT
55
bike3.png
5
toulouse
11/02/2020 11:44
10
3
Skygrey
RTT
55
bike3.png
[Bicycall.Rent, Bicycall.Rent]
10
faresbenslama95@gmail.com
10
faresbenslama95@gmail.com
123456
benslama
fares
12345671
["user_id": 10, "lastname": "fares", "email": "faresbenslama95@gmail.com", "phone": "12345671", "password": "123456", "name": "benslama"]
<NSHTTPURLResponse: 0x600003854240> { URL: http://localhost:3000/locations } { Status Code: 200, Headers {
    Connection =     (
        "keep-alive"
    );
    "Content-Length" =     (
        339
    );
    "Content-Type" =     (
        "text/html; charset=utf-8"
    );
    Date =     (
        "Mon, 14 Dec 2020 15:50:15 GMT"
    );
    Etag =     (
        "W/\"153-s80IUuqdLC0Rkz3oIqCM7x8xP7I\""
    );
    "Keep-Alive" =     (
        "timeout=5"
    );
    "X-Powered-By" =     (
        Express
    );
} }
339 bytes
[Bicycall.Rents, Bicycall.Rents]
3
Klibia
15/10/2020 15:11
10
3
Skygrey
RTT
55
bike3.png
5
toulouse
11/02/2020 11:44
10
3
Skygrey
RTT
55
bike3.png
[Bicycall.Rent, Bicycall.Rent]
<NSHTTPURLResponse: 0x6000038490a0> { URL: http://localhost:3000/bikes } { Status Code: 200, Headers {
    Connection =     (
        "keep-alive"
    );
    "Content-Length" =     (
        314
    );
    "Content-Type" =     (
        "application/json; charset=utf-8"
    );
    Date =     (
        "Mon, 14 Dec 2020 15:50:18 GMT"
    );
    Etag =     (
        "W/\"13a-G0h70hatF47P4CgaOr94LtJs2ug\""
    );
    "Keep-Alive" =     (
        "timeout=5"
    );
    "X-Powered-By" =     (
        Express
    );
} }
314 bytes
bike.png
http://localhost:3000/bike.png
bike2.png
http://localhost:3000/bike2.png
bike3.png
http://localhost:3000/bike3.png
bike4.png
http://localhost:3000/bike4.png
[Bicycall.Bike, Bicycall.Bike, Bicycall.Bike, Bicycall.Bike]
10
faresbenslama95@gmail.com
10
faresbenslama95@gmail.com
123456
benslama
fares
12345671
["user_id": 10, "email": "faresbenslama95@gmail.com", "lastname": "fares", "name": "benslama", "password": "123456", "phone": "12345671"]
<NSHTTPURLResponse: 0x600003848300> { URL: http://localhost:3000/locations } { Status Code: 200, Headers {
    Connection =     (
        "keep-alive"
    );
    "Content-Length" =     (
        339
    );
    "Content-Type" =     (
        "text/html; charset=utf-8"
    );
    Date =     (
        "Mon, 14 Dec 2020 15:50:20 GMT"
    );
    Etag =     (
        "W/\"153-s80IUuqdLC0Rkz3oIqCM7x8xP7I\""
    );
    "Keep-Alive" =     (
        "timeout=5"
    );
    "X-Powered-By" =     (
        Express
    );
} }
339 bytes
[Bicycall.Rents, Bicycall.Rents]
3
Klibia
15/10/2020 15:11
10
3
Skygrey
RTT
55
bike3.png
5
toulouse
11/02/2020 11:44
10
3
Skygrey
RTT
55
bike3.png
[Bicycall.Rent, Bicycall.Rent]
    @IBOutlet weak var tableView: UITableView!{
        didSet {
            tableView.delegate = self
            tableView.dataSource = self
        }
    }
    DispatchQueue.main.async {[weak self] in
        self?.tableView.reloadData()
    }
        session.dataTask(with: request) {[weak self] (data,response,error) in