【问题标题】:iOS - Print and store array of dictionary in swift 3iOS - 在swift 3中打印和存储字典数组
【发布时间】:2017-03-31 15:00:05
【问题描述】:

在我的项目中,我有一个字典数组:

var list = [[String: Any]] ()

非常简单,我所做的是从 URL 下载 JSON 文件

//        json parsing 2
        let url:String = "https://jsonplaceholder.typicode.com/users"
        let urlRequest = URL(string: url)
        URLSession.shared.dataTask(with: urlRequest!, completionHandler: {(data, response, error) in
            if(error != nil)
            {
                print(error.debugDescription)
            }else{
                do {
                    self.list = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
                        as! [[String: Any]]
                    OperationQueue.main.addOperation {
                        self.tableView.reloadData()
                    }
                }catch let error as NSError{
                    print(error)
                }
            }
        }).resume()

现在,在 list(这是一个字典数组)中,我拥有了我感兴趣的所有数据。但是,如果我想选择一个值,或者打印 list 的全部内容,我应该怎么做?

编辑:另外,我可以简化结构而不使用数组 字典,但更简单的东西?

【问题讨论】:

  • 需要更多信息。如果您在示例中写下您想要解析的 JSON 以及您想用它做什么,也许我们可以向您推荐解决方案。
  • 但是对于单元格来说很简单,就像 self.list[indexPath.row]["yourKey"]
  • 请看一下 SwiftyJSON。 github.com/SwiftyJSON/SwiftyJSON 它将简化此类任务。
  • @AitorPagán 我下载的json文件,我必须在tableView中表示,这部分我已经在tableView的func中用let item = self. list [indexPath.row] Mobile textLabel?. text = this ["name"] as? String Mobile detailTextLabel?. text = this ["email"] as? String解决了。我的问题是打印字典数组或访问它
  • @Matteo 正如@Retterdesdialogs 所说,您可以通过数组的索引访问它们。但是我要说的是,如果您可以编写 JSON 只是为了参考您正在使用的内容。可以使用打印方法print(array)进行打印

标签: ios arrays dictionary


【解决方案1】:

或者你可以使用归档

import Foundation

class UserDataManager {

var documentsURL: NSURL?
var fileURL:NSURL?

func setFileURL(file: String) {
    documentsURL = FileManager.default.urls(for: .documentDirectory,
                                                   in: .userDomainMask)[0] as NSURL?
    fileURL = documentsURL!.appendingPathComponent(file) as NSURL?

}

func write(file: String, mDict: NSDictionary) {
    setFileURL(file: file)
    let data : NSData = NSKeyedArchiver.archivedData(withRootObject: mDict) as NSData
    if let fileAssigend = fileURL {
        data.write(to: fileAssigend as URL, atomically: true)
    }
}

func read(file: String) -> NSDictionary {
    setFileURL(file: file)

    var data:NSData?
    if let url = fileURL {
        data =  NSData(contentsOf: url as URL)
        if let result = data {
            return NSKeyedUnarchiver.unarchiveObject(with: result as Data) as! NSDictionary
        }
    }

    let dict = NSDictionary()

    return dict
}
}

并与它一起使用

写:

        let userDataManager = UserDataManager()
        userDataManager.write(file: "userDataDict", mDict: responseObject)

阅读:

        let userDataManager = UserDataManager()
        let userDataDict = userDataManager.read(file: "userDataDict")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2020-06-19
    • 2018-01-05
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多