SWIFT:字典里面修改阵列 [英] swift: modifying arrays inside dictionaries

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

问题描述

我怎么能轻易元素添加到字典里的数组?
它总是与抱怨找不到成员追加找不到'+ ='

How can I easily add elements to an array inside a dictionary? It's always complaining with could not find member 'append' or could not find an overload for '+='

var dict = Dictionary<String, Array<Int>>()
dict["key"] = [1, 2, 3]

// all of these fail
dict["key"] += 4
dict["key"].append(4) // xcode suggests dict["key"].?.append(4) which also fails
dict["key"]!.append(4)
dict["key"]?.append(4)

// however, I can do this:
var arr = dict["key"]!
arr.append(4) // this alone doesn't affect dict because it's a value type (and was copied)
dict["key"] = arr

如果我只是数组分配给一个变​​种,修改,然后将其重新分配给字典,也不会被我抄袭的一切吗?那会不会是有效也不优雅。

if I just assign the array to a var, modify it and then reassign it to the dict, won't I be copying everything? that wouldn't be efficient nor elegant.

推荐答案

雨燕测试5增加了这个功能,你已经钉在一对夫妇的尝试的新方法。该解缠运营商现在通过价值要么运营商或方法调用。也就是说,你可以在任何的这些方式添加到该数组:

Swift beta 5 has added this functionality, and you've nailed the new method in a couple of your attempts. The unwrapping operators ! and ? now pass through the value to either operators or method calls. That is to say, you can add to that array in any of these ways:

dict["key"]! += [4]
dict["key"]!.append(4)
dict["key"]?.append(4)

与往常一样,要小心你使用哪个运营商 - 力展开的值,是不是在你的字典会给你一个运行时错误:

As always, be careful about which operator you use -- force unwrapping a value that isn't in your dictionary will give you a runtime error:

dict["no-key"]! += [5]        // CRASH!

而使用可选的链接会默默地失败:

Whereas using optional chaining will fail silently:

dict["no-key"]?.append(5)     // Did it work? Swift won't tell you...

在理想情况下你可以使用新的空合并运算符 ?? 来解决这个第二种情况,但现在这不是工作。

Ideally you'd be able to use the new null coalescing operator ?? to address this second case, but right now that's not working.

从回答pre-雨燕测试5:

Answer from pre-Swift beta 5:

这是斯威夫特的怪癖,它是不可能做你想要做的事。问题是,中的的任何可选变量实际上是在一个恒定的 - 即使强行展开。如果我们只是定义了一个可选的阵列,这里就是我们能做和不能做的:

It's a quirk of Swift that it's not possible to do what you're trying to do. The issue is that the value of any Optional variable is in fact a constant -- even when forcibly unwrapping. If we just define an Optional array, here's what we can and can't do:

var arr: Array<Int>? = [1, 2, 3]
arr[0] = 5
// doesn't work: you can't subscript an optional variable
arr![0] = 5
// doesn't work: constant arrays don't allow changing contents
arr += 4
// doesn't work: you can't append to an optional variable
arr! += 4
arr!.append(4)
// these don't work: constant arrays can't have their length changed

您遇到的麻烦字典的原因是,下标字典返回一个可选值,因为没有保证该词典将有关键。因此,在字典中的数组具有相同的行为作为可选的阵列,上面:

The reason you're having trouble with the dictionary is that subscripting a dictionary returns an Optional value, since there's no guarantee that the dictionary will have that key. Therefore, an array in a dictionary has the same behavior as the Optional array, above:

var dict = Dictionary<String, Array<Int>>()
dict["key"] = [1, 2, 3]
dict["key"][0] = 5         // doesn't work
dict["key"]![0] = 5        // doesn't work
dict["key"] += 4           // uh uh
dict["key"]! += 4          // still no
dict["key"]!.append(4)     // nope

如果你需要改变的东西在字典中的一个数组,你需要得到数组的一个副本,改变它,并重新分配,像这样的:

If you need to change something in an array in the dictionary you'll need to get a copy of the array, change it, and reassign, like this:

if var arr = dict["key"] {
    arr.append(4)
    dict["key"] = arr
}

ETA:相同的技术工作在斯威夫特Beta 3中,虽然不断的阵列不再允许更改内容。

ETA: Same technique works in Swift beta 3, though constant arrays no longer allow changes to contents.

这篇关于SWIFT:字典里面修改阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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