为什么无法在 Swift 项目中访问我的模型类? [英] Why am not able to access my model class in Swift Project?

查看:45
本文介绍了为什么无法在 Swift 项目中访问我的模型类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 ViewController 访问我的模型并使用模型数据加载到表视图中????

源代码链接

我的 ViewController 看起来像这样

My ViewController looks like this

import UIKit

class ViewController: UIViewController {
    var cclm: CountryCodeListModel?

    override func viewDidLoad() {
        super.viewDidLoad()
        Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(hello), userInfo: nil, repeats: true)
        readLocalJSONFile(forName: "countryList")

        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(_ animated: Bool) {
        
    }
    
    @objc func hello()
     {
        print(cclm?.data?[0].flag)
     }

}

我的模型类看起来像这样

and my model class look like this

struct CountryCodeList : Decodable {
    var alpha2Code: String?
    var alpha3Code: String?
    var flag      : String?
    var name      : String?
    var code      : String?
}

public struct CountryCodeListModel : Decodable {
    var data      : [CountryCodeList]?
}

var cclm: CountryCodeListModel?


//Method to load json

func readLocalJSONFile(forName name: String) {
    do {
        if let filePath = Bundle.main.path(forResource: name, ofType: "json") {
            let fileUrl = URL(fileURLWithPath: filePath)
            let data = try Data(contentsOf: fileUrl)
            if let countryCodeObject = parse(jsonData: data) {
                cclm = countryCodeObject
                print(cclm?.data?[1].alpha2Code ?? "")  //Printing Correct Value
            }
        }
    } catch {
        print("error: \(error)")
    }
}



func parse(jsonData: Data) -> CountryCodeListModel?{
    var dataArray : [Dictionary<String,Any>] = [[:]]
    var country = Dictionary<String,Any>()
    var modelData = Dictionary<String,Any>()
    do {
        // make sure this JSON is in the format we expect
        if let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as? Dictionary<String,Any> {
            dataArray.removeAll()
            for item  in json["data"] as! [Dictionary<String, Any>] {
                country = item
                
                let url = URL(string: country["flag"] as? String ?? "")
                let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
                let image = UIImage(data: data!)
                let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let fileName = url?.lastPathComponent // name of the image to be saved
                let fileURL = documentsDirectory.appendingPathComponent(fileName ?? "")
                if let data = image?.jpegData(compressionQuality: 1.0){
                    do {
                        try data.write(to: fileURL)
                        country["flag"] = fileURL.absoluteString
                        //print("file saved")
                        //urlAsString = fileURL.absoluteString
                    } catch {
                        print("error saving file:", error)
                    }
                }
                
                dataArray.append(country)
                country.removeAll()
                
                    
                
            }
            modelData["data"] = dataArray
            //print(modelData)
            let jsonData1 = try JSONSerialization.data(withJSONObject: modelData, options: [])
            
            do {
                    let decodedData = try JSONDecoder().decode(CountryCodeListModel.self, from: jsonData1)
                
                    return decodedData
                } catch {
                    print("error: \(error)")
                }
            
        }
    } catch let error as NSError {
        print("Failed to load: \(error.localizedDescription)")
    }
    return nil
}

问题说明:

我正在读取本地json并获取标志键的url值并将相应的图像下载到本地.下载后,我将使用本地路径并在字典中更新,然后创建 JSON 对象并更新我的模型类.

Iam reading local json and take the url value of flag key and download corresponding images to local. Once i download then am taking the localpath and update in the dictionary and then create JSON object and update my model class.

现在,我正在尝试从 ViewController 访问我的模型类,如下所示

Now, am trying to access my model class from ViewController like below

print(CountryCodeListModel?.data?[0].name) //check screenshot for error
 print(cclm?.data?[0].flag)                 // this prints nil always

请检查附件中的错误截图2

Please check the error screenshots attached2

我的 JSON 看起来像这样

My JSON look like this

{
   "meta":{
      "success":true,
      "message":"Successfully retrieved country details",
      "code":"200"
   },
   "data":[
      {
         "alpha2Code":"AF",
         "alpha3Code":"AFG",
         "flag":"https://raw.githubusercontent.com/DevTides/countries/master/afg.png",
         "name":"Afghanistan",
         "code":"+93"
      },
      {
         "alpha2Code":"AX",
         "alpha3Code":"ALA",
         "flag":"https://raw.githubusercontent.com/DevTides/countries/master/ala.png",
         "name":"Aland Islands",
         "code":"+358"
      },
      {
         "alpha2Code":"AL",
         "alpha3Code":"ALB",
         "flag":"https://raw.githubusercontent.com/DevTides/countries/master/alb.png",
         "name":"Albania",
         "code":"+355"
      },
      {
         "alpha2Code":"DZ",
         "alpha3Code":"DZA",
         "flag":"https://raw.githubusercontent.com/DevTides/countries/master/dza.png",
         "name":"Algeria",
         "code":"+213"
      },
      {
         "alpha2Code":"AS",
         "alpha3Code":"ASM",
         "flag":"https://raw.githubusercontent.com/DevTides/countries/master/asm.png",
         "name":"American Samoa",
         "code":"+1684"
      }
]
}

推荐答案

您正在尝试解码不存在的东西.

You are trying to decode something that doesn't exist.

print(CountryCodeListModel?.data?[0].name) //check screenshot for error
 print(cclm?.data?[0].flag)                 // this prints nil always

上面的代码说明你想要:

The above code states that you want:

  1. 名字
  2. 位置 0 处的变量数据
  3. 结构 CountryCodeListModel.

你想做的是:

  1. 名字
  2. 位置 0 处的变量
  3. 结构体 CountryCodeListModel 的实例.

例如...

func readLocalJSONFile(forName name: String) {
    do {
        if let filePath = Bundle.main.path(forResource: name, ofType: "json") {
            let fileUrl = URL(fileURLWithPath: filePath)
            let data = try Data(contentsOf: fileUrl)
            if let countryCodeObject = parse(jsonData: data) {
                cclm = countryCodeObject
                print(cclm?.data?[1].alpha2Code ?? "")  //Printing Correct Value
                print(cclm?.data?[0].flag ?? "")
                print(countryCodeObject?.data[0].flag ?? "") // Same as the line above
            }
        }
    } catch {
        print("error: \(error)")
    }
}

除非您尝试使用 static 变量(在该变量处您将使用 CountryCodeListModel.data),否则您需要确保您实际使用的是结构或类的对象来引用您的属性.

Unless you are trying to use a static variable (at which you would use CountryCodeListModel.data), you need to make sure you are actually using an instance of the structure or an object of a class to reference your properties.

注意

CountryCodeListModel 是一个结构体.CountryCodeListModel() 是结构 CountryCodeListModel 的一个实例.由于您可以拥有一个结构的多个实例,因此在访问数据时需要引用特定的结构.因此,CountryCodeListModel.data 将不起作用,它需要是 CountryCodeListModel().data.在这种情况下,您有 cclm.data.

CountryCodeListModel is a structure. CountryCodeListModel() is an instance of the structure CountryCodeListModel. Since you can have multiple instances of a structure, you need to reference a specific structure when accessing data. Thus, CountryCodeListModel.data will not work and it needs to be CountryCodeListModel().data. In this case, you have cclm.data.

这篇关于为什么无法在 Swift 项目中访问我的模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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