英文:
have call api data and Fateh in table view cell
问题
我已运行此代码,但没有在控制台打印任何数据,也没有从我的API获取数据,因此模拟器中没有显示任何数据。
func downloadJSON(completed: @escaping () -> ()){
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) {data, response, error in
guard let data = data, error == nil else {return}
//print(data)
if error == nil {
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
print(json)
self.detail = try JSONDecoder().decode([apiData].self, from: data)
DispatchQueue.main.async {
completed()
}
}
}catch{
print("JSON Error")
}
}
}.resume()
}
请注意,我只翻译了代码部分,没有包括注释。如果您需要更多帮助或有其他问题,请告诉我。
英文:
i have run this code but not print any data in console and not get data in my api so no data display in may simulators
func downloadJSON(completed: @escaping () -> ()){
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) {data, response, error in
guard let data = data, error == nil else {return}
//print(data)
if error == nil {
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
print(json)
self.detail = try JSONDecoder().decode([apiData].self, from: data)
DispatchQueue.main.async {
completed()
}
}
}catch{
print("JSON Error")
}
}
}.resume()
}
}
func downloadJSON(completed: @escaping () -> ()){
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) {data, response, error in
guard let data = data, error == nil else {return}
//print(data)
if error == nil {
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]{
print(json)
self.detail = try JSONDecoder().decode([apiData].self, from: data)
DispatchQueue.main.async {
completed()
}
}
}catch{
print("JSON Error")
}
}
}.resume()
}
}
答案1
得分: 1
以下是您要翻译的代码部分:
class ViewController: UIViewController {
// MARK: - Iboutlet
@IBOutlet weak var apiDataTableView: UITableView!
// MARK: - Varaible
var apiData = [DataModel]()
// MARK: - Life CYcle Method
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
intlizer()
}
// MARK: - Intlization
func intlizer() {
downloadJSON {
self.apiDataTableView.reloadData()
}
}
// MARK: - Make A Api Call
func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) { data, response, error in
guard let data = data, error == nil else { return }
// print(data)
if error == nil {
do {
let decoder = JSONDecoder()
let decodedData = try decoder.decode(ApiDataModel.self, from: data)
self.apiData = decodedData.message
print(self.apiData)
DispatchQueue.main.async {
completed()
}
} catch {
print("JSON Error")
}
}
}.resume()
}
}
如果您需要更多数据,可以添加相应的变量并使用与API数据的名称相同的名称。
此外,以下是您的模型代码:
// MARK: - Model
struct ApiDataModel: Decodable {
var success: Bool
var message: [DataModel]
}
struct DataModel: Decodable {
var id: String
var ip_address: String
var access_token: String
}
如果您不想使用与API数据名称相同的变量名,您可以像这样编写:
struct DataModel: Decodable {
var id: String
var ip_address: String
var access_token: String
enum CodingKeys: String, CodingKey {
case id
case ipAddress = "ip_address"
case accessToken = "access_token"
}
}
您还提供了关于您的表格视图屏幕的截图,但这不是可翻译的文本。如果您有其他需要翻译的文本,请随时提问。
英文:
class ViewController: UIViewController {
// MARK: - Iboutlet
@IBOutlet weak var apiDataTableView: UITableView!
// MARK: - Varaible
var apiData = [DataModel]()
// MARK: - Life CYcle Method
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
intlizer()
}
// MARK: - Intlization
func intlizer() {
downloadJSON {
self.apiDataTableView.reloadData()
}
}
// MARK: - Make A Api Call
func downloadJSON(completed: @escaping () -> ()){
let url = URL(string: "https://gydo.me/api/api/User/index")
URLSession.shared.dataTask(with: url!) {data, response, error in
guard let data = data, error == nil else {return}
//print(data)
if error == nil {
do{
let decoder = JSONDecoder()
let decodedData = try
decoder.decode(ApiDataModel.self, from: data)
self.apiData = decodedData.message
print(self.apiData)
DispatchQueue.main.async {
completed()
}
} catch {
print("JSON Error")
}
}
}.resume()
}
}
above is my viewcontroller
> This is my model for api data
// MARK: - Model
struct ApiDataModel: Decodable {
var success: Bool
var message: [DataModel]
}
struct DataModel: Decodable {
var id: String
var ip_address: String
var access_token: String
}
if you want more data then you can add variable and use name same as api data name
> if you don't want to use same as api data name so you can write like this
struct DataModel: Decodable {
var id: String
var ip_address: String
var access_token: String
enum Codingkeys: String, CodingKey {
case id
case ipAddress = "ip_address"
case accessToken = "access_token"
}
}
> And My tableView Screen Is Below
I Hope You Need This
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论