追加到 [String: Any] 字典结构中的数组 [英] Append to array in [String: Any] dictionary structure

查看:33
本文介绍了追加到 [String: Any] 字典结构中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

组装传递给 GRMustache.swift 的数据负载以呈现胡须模板,我正处于需要的场景中将数据附加到先前在字典中定义的数组中.

我的数据结构开始于:

var 数据:[字符串:任意] = ["key1": "示例值 1","key2": "示例值 2",项目": [//我需要稍后在这里追加]]

items 键对是我稍后需要在循环中附加的集合.

要添加到 data["items"] 数组,我正在尝试以下操作:

for index in 1...3 {让项目:[字符串:任何] = [键":新值"]数据[项目"].append(项目)}

此错误,因为 Any? 类型的值没有成员 append,并且二元运算符 += 不能应用于类型的操作数Any?[String : Any].

这是有道理的,因为我需要转换要追加的值;但是,我无法改变数组.

强制转换为数组,是否强制向下转换会报错:

(data["items"] as! Array).append(item)

<块引用>

'有吗?'不可转换为 'Array<_>';你的意思是使用作为!"强迫情绪低落?

不能对Array<_>"类型的不可变值使用可变成员

好像我的演员阵容错了;或者,也许我的做法是错误的.

关于如何随着时间的推移迭代地填充 data["items"] 有什么建议吗?

解决方案

data[Items] 的类型不是 Array 而是 Array<[字符串:任意]>.

您可能可以将其压缩成更少的步骤,但我更喜欢多个步骤的清晰度:

var 数据:[字符串:任意] = ["key1": "示例值 1","key2": "示例值 2",项目": []]对于 1...3 中的索引 {让项目:[字符串:任何] = [键":新值"]//获取现有项目,如果不存在则创建新数组var existingItems = data["items"] as?[[字符串:任意]] ??[[字符串:任意]]()//附加项目existingItems.append(item)//替换回 `data`数据["items"] = existingItems}

Assembling a data payload passed to GRMustache.swift for rendering mustache templates, I'm in a scenario where I need to append data to an array previously defined in the dictionary.

My data structure starts off as:

var data: [String: Any] = [
    "key1": "example value 1",
    "key2": "example value 2",
    "items": [
        // I need to append here later
    ]
]

The items key pair is a collection I need to append later within a loop.

To add to the data["items"] array, I'm trying something like:

for index in 1...3 {
    let item: [String: Any] = [
        "key": "new value"
    ]

    data["items"].append(item)
}

This errors, as value of type Any? has no member append, and binary operator += cannot be applied to operands of type Any? and [String : Any].

This makes sense, as I need to cast the value to append; however, I can't mutate the array.

Casting to array, whether forcing downcast gives the error:

(data["items"] as! Array).append(item)

'Any?' is not convertible to 'Array<_>'; did you mean to use 'as!' to force downcast?

Cannot use mutating member on immutable value of type 'Array<_>'

Seems like my cast is wrong; or, perhaps I'm going about this in the wrong way.

Any recommendation on how to fill data["items"] iteratively over time?

解决方案

The type of data[Items] isn't Array but actually Array<[String: Any]>.

You could probably squeeze this into fewer steps, but I prefer the clarity of multiple steps:

var data: [String: Any] = [
    "key1": "example value 1",
    "key2": "example value 2",
    "items": []
]

for index in 1...3 {

    let item: [String: Any] = [
        "key": "new value"
    ]

    // get existing items, or create new array if doesn't exist
    var existingItems = data["items"] as? [[String: Any]] ?? [[String: Any]]()

    // append the item
    existingItems.append(item)

    // replace back into `data`
    data["items"] = existingItems
}

这篇关于追加到 [String: Any] 字典结构中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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