简体   繁体   English

如何在Swift中进行嵌套字典

[英]How to do nested dictionaries in Swift

As a beginner to Swift I'm struggling with even the simplest things :( I've tried in so many ways to create a dictionary within a dictionary but it always fails. 作为Swift的初学者,即使是最简单的事情我也在努力:(我已经尝试过很多方法在字典中创建字典,但总是失败。

The reason is a I want to fill a table with 10 items. 原因是我要填写一个包含10个项目的表格。 Each item has a title, url, pageviews and unique users. 每个项目都有标题,网址,综合浏览量和唯一身份用户。 String, string, int, int. String,string,int,int。 I want to be able to both read the data and fill the tablecell by using IndexPath.row and I want to be able to update the data every 5th second based on input from a api with json content. 我希望能够通过使用IndexPath.row读取数据并填充tablecell,并且我希望能够基于来自带有json内容的api的输入每5秒更新一次数据。

I was trying in Playground and ended up with: 我在Playground尝试并最终得到:

var topArticleDetails:[String:AnyObject] = ["title":"","url":"","uniqueUsers":0,"events":0]
var topArticles:[Int:NSDictionary] = [0:topArticleDetails]

topArticleDetails["title"] = "Varden - Nyheter"
topArticleDetails["url"] = "http://www.varden.no/nyheter"
topArticleDetails["uniqueUsers"] = 50
topArticleDetails["events"] = 100

topArticles[1] = topArticleDetails

and it seems to work fine, but when I add this to my ViewController I'm getting the error Instance member 'topArticleDetails' cannot be used on type 'ViewController' 它似乎工作正常,但当我将其添加到我的ViewController我得到错误实例成员'topArticleDetails'不能用于类型'ViewController'

Close but no cigar.. 关闭但没有雪茄..

How can I solve this? 我怎么解决这个问题? Maybe there is a better way than the one I'm trying now. 也许有一种比我现在尝试的更好的方式。 At the moment it's kinda working but instead of having a nested dictionary I've ended up with four seperate arrays like this: 目前它有点工作,但没有嵌套字典,我最终得到了四个单独的数组,如下所示:

var topArticlesTitle = [String]()
var topArticlesURL = [String]()
var topArticlesEvents = [Int]()
var topArticlesUniqueUsers = [Int]()

and I'm pretty sure there must be a better way. 而且我很确定必须有更好的方法。

>> UPDATE << >>更新<<

In addition to the solutions provided below I found another one. 除了下面提供的解决方案,我发现了另一个。 It's so simple I'm ashamed but I guess it's a part of learning both Swift and OOP at the same time. 这很简单,我很惭愧,但我想这是同时学习Swift和OOP的一部分。 If I removed the data I tried to enter into the variables when declaring them and just kept them empty it worked like a charm. 如果我删除了数据,我试图在声明它们时输入变量并将它们保持为空它就像一个魅力。

var topArticleDetails = [String:AnyObject]()
var topArticles = [Int:NSDictionary]()

Instead of using a dictionary inside a dictionary, you can use and struct array, for example: 您可以使用和struct数组,而不是在字典中使用字典,例如:

struct Article {
    var title: String?
    var url: String?
    var: events: Int?
    var uniqueUsers: Int?
}

var topArticles = [Article]()

It is more easy to mantain than the dictionary inside dictionary because it will be easy to change your model. 它比字典中的字典更容易保留,因为它很容易改变你的模型。

To answer your question about nested Dictionaries (I would go with pableiros's answer though for not abusing dictionaries, structs work better) you can declare them like this: 要回答你关于嵌套字典的问题(我会用pableiros的答案,但是为了不滥用字典,结构更好)你可以像这样声明它们:

var test = [Int: [String: AnyObject]]()

To convert JSON, you can use the NSJSONSerialization class: 要转换JSON,您可以使用NSJSONSerialization类:

try NSJSONSerialization.JSONObjectWithData(your_encoded_json, options: []) as! [Int: [String: AnyObject]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM