删除数组内部的项,该数组是字典Swift 2中的值 [英] Remove item that is inside of an array which is the value in a dictionary Swift 2

查看:200
本文介绍了删除数组内部的项,该数组是字典Swift 2中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前可能已经回答过这个问题,但是我搜索时找不到任何东西.

I know this might have been answered before, but I couldn't find anything when i searched.

所以我有一个像这样的字典:

So i have a dictionary that looks like this:

var dict = [String:[String]]()

我想做的是删除数组内部的某个索引(字典的值).假设我要从以下代码中删除字符串"Chair":

What I want to do is to remove a certain index inside of the array (the dictionary's value). Say I want to remove the string "Chair" from this code:

dict = ["Furniture": ["Table", "Chair", "Bed"], "Food": ["Pancakes"]]

对不起,如果已经回答了.

Sorry again if this has already been answered.

推荐答案

许多涵盖字典条目突变的答案都倾向于关注 删除值->突变值->替换价值这个成语,但请注意,删除并不是必需的.您也可以使用可选链接

Many answers that cover the mutation of dictionary entries tend to focus on the remove value -> mutate value -> replace value idiom, but note that removal is not a necessity. You can likewise perform an in-place mutation using e.g. optional chaining

dict["Furniture"]?.removeAtIndex(1)

print(dict)
    /* ["Furniture": ["Table", "Bed"], 
        "Food": ["Pancakes"]]          */

但是请注意,使用.removeAtIndex(...)解决方案并不完全安全,除非您执行数组边界检查,并确保确实存在我们尝试为其删除元素的索引.

Note however that using the .removeAtIndex(...) solution is not entirely safe, unless you perform an array bounds check, making sure that the index for which we attempt to remove an element actually exists.

作为一种安全的就地突变方法,请使用可选绑定语句的where子句来检查我们要删除的索引是否未超出范围

As a safe in-place-mutation alternative, use the where clause of an optional binding statement to check the the index we want to remove is not out of bounds

let removeElementAtIndex = 1
if let idxs = dict["Furniture"]?.indices where removeElementAtIndex < idxs.endIndex {
    dict["Furniture"]?.removeAtIndex(removeElementAtIndex)
}

另一个安全的替代方法是使用advancedBy(_:limit)获取要在removeAtIndex(...)中使用的安全索引.

Another safe alternative is making use of advancedBy(_:limit) to get a safe index to use in removeAtIndex(...).

let removeElementAtIndex = 1
if let idxs = dict["Furniture"]?.indices {
    dict["Furniture"]?.removeAtIndex(
        idxs.startIndex.advancedBy(removeElementAtIndex, limit: idxs.endIndex))
}  

最后,如果使用 remove/mutate/replace 习惯用法,另一种安全的替代方法是使用flatMap进行变异步骤,如果给定索引中存在该元素,则删除该元素大批.例如,对于通用方法(和where子句滥用:)

Finally, if using the remove/mutate/replace idiom, another safe alternative is using a flatMap for the mutating step, removing an element for a given index, if that index exists in the array. E.g., for a generic approach (and where clause abuse :)

func removeSubArrayElementInDict<T: Hashable, U>(inout dict: [T:[U]], forKey: T, atIndex: Int) {
    guard let value: [U] = dict[forKey] where
        { () -> Bool in dict[forKey] = value
            .enumerate().flatMap{ $0 != atIndex ? $1 : nil }
            return true }()
        else { print("Invalid key"); return }
}

/* Example usage */
var dict = [String:[String]]()
dict = ["Furniture": ["Table", "Chair", "Bed"], "Food": ["Pancakes"]]

removeSubArrayElementInDict(&dict, forKey: "Furniture", atIndex: 1)

print(dict)
    /* ["Furniture": ["Table", "Bed"], 
        "Food": ["Pancakes"]]          */

这篇关于删除数组内部的项,该数组是字典Swift 2中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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