修改字典斯威夫特数组 [英] Modifying an array of dictionaries in Swift

查看:105
本文介绍了修改字典斯威夫特数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的斯威夫特和一直有一些麻烦搞清楚数组和字典的某些方面。

I’m new to Swift and have been having some troubles figuring out some aspects of Arrays and Dictionaries.

我有字典的数组,对此我已经使用类型别名 - 例如

I have an array of dictionaries, for which I have used Type Aliases - e.g.

typealias myDicts = Dictionary<String, Double>

var myArray : [myDicts] = [
["id":0,
    "lat":55.555555,
    "lng":-55.555555,
    "distance":0],
["id":1,
    "lat": 44.444444,
    "lng":-44.444444,
    "distance":0]
]

然后,我想通过字典数组中的迭代和修改的距离的键值。我做这样的:

I then want to iterate through the dictionaries in the array and change the "distance" key value. I did it like this:

for dict:myDicts in myArray {

dict["distance"] = 5

}

甚至专门确保5与许多不同的方法,包括一个双例如。

Or even specifically making sure 5 is a double with many different approaches including e.g.

for dict:myDicts in myArray {

let numberFive : Double = 5

dict["distance"] = numberFive

}

我所有的尝试导致错误:

All my attempts cause an error:

 @lvalue $T5' is not identical to '(String, Double)

这似乎是充当如果字典里是永恒不变的让,而不是变种。所以笔者随机尝试这样做:

It seems to be acting as if the Dictionaries inside were immutable "let" rather than "var". So I randomly tried this:

for (var dict:myDicts) in myArray {

dict["distance"] = 5

}

这消除了错误,关键确实是分配5内循环,但是这似乎并没有真正修改数组本身的长远目标。我在做什么错了?

This removes the error and the key is indeed assigned 5 within the for loop, but this doesn't seem to actually modify the array itself in the long run. What am I doing wrong?

推荐答案

你没有做错什么。这就是雨燕是如何工作的。你有两个选择:

You're not doing anything wrong. That's how Swift works. You have two options:


  • 使用的NSMutableDictionary,而不是斯威夫特字典。

  • Use NSMutableDictionary rather than a Swift dictionary.

使用自定义类,而不是一本字典。在某种程度上,这是一个更好的解决办法呢,因为这是你应该已经沿的情况下所有的字典具有相同的结构做的所有东西。

Use a custom class instead of a dictionary. In a way this is a better solution anyway because it's what you should have been doing all along in a situation where all the dictionaries have the same structure.

自定义类我谈论的将是一个单纯的值类性质的捆绑。这是一种痛苦在Objective-C进行的,但是在斯威夫特是微不足道,所以我现在这样做了很多。问题是,你能坚持随时随地为您的自定义类的类定义;它并不需要它自己的文件,当然在斯威夫特你不具备的接口/实现富加以解决的,更不用说内存管理和其他的东西。因此,这是code的只是几行,你能坚持正确的与code你已经有了。

The "custom class" I'm talking about would be a mere "value class", a bundle of properties. This was kind of a pain to make in Objective-C, but in Swift it's trivial, so I now do this a lot. The thing is that you can stick the class definition for your custom class anywhere; it doesn't need a file of its own, and of course in Swift you don't have the interface/implementation foo to grapple with, let alone memory management and other stuff. So this is just a few lines of code that you can stick right in with the code you've already got.

下面是从我自己的code的例子:

Here's an example from my own code:

class Model {
    var task : NSURLSessionTask!
    var im : UIImage!
    var text : String!
    var picurl : String!
}

我们再有型号的数组,我们就走了。

We then have an array of Model and away we go.

所以,在你的例子:

class MyDict : NSObject {
    var id = 0.0
    var lat = 0.0
    var lng = 0.0
    var distance = 0.0
}

var myArray = [MyDict]()

let d1 = MyDict()
d1.id = 0
d1.lat = 55.55
d1.lng = -55.55
d1.distance = 0
let d2 = MyDict()
d2.id = 0
d2.lat = 44.44
d2.lng = -44.44
d2.distance = 0
myArray = [d1,d2]

// now we come to the actual heart of the matter

for d in myArray {
    d.distance = 5
}
println(myArray[0].distance) // it worked
println(myArray[1].distance) // it worked

这篇关于修改字典斯威夫特数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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