【问题标题】:swift ios append elements to arrayswift ios将元素附加到数组
【发布时间】:2016-12-22 04:50:05
【问题描述】:

我有一个如下所示的数组:

var parameters: [String: AnyObject] = [
            "title": "something",
            "type": "1"
        ]

如何添加这样的内容:

"someNewField": "someValue"

这样数组最终会变成这样:

   parameters: [String: AnyObject] = [
                "title": "something",
                "type": "1",
                "someNewField": "someValue"
            ]

【问题讨论】:

  • 这可能是一个更适合 Struct 的用例,仅通过 Dictionary 键来判断。

标签: ios arrays swift xcode append


【解决方案1】:

这是一个字典而不是一个数组。要为其添加新内容,您可以执行以下操作:

parameters["someNewField"] = "someValue"

您可以在此处找到有关数组、字典及其差异的更多文档:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

这也是一个可能的重复:

How to append elements into dictionary in swift?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2018-08-18
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    相关资源
    最近更新 更多