Remove item that is inside of an array which is the value in a dictionary Swift 2

后端 未结 3 451
余生分开走
余生分开走 2021-01-25 01:38

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 = [Stri         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 02:25

    If you want to remove specific element, you could do this:

    var dict = ["Furniture": ["Table", "Chair", "Bed"], "Food": ["Pancakes"]]
    
    extension Array where Element: Equatable {
        mutating func removeElement(element: Element) {
            if let index = indexOf ({ $0 == element }) {
                removeAtIndex(index)
            }
        }
    }
    
    dict["Furniture"]?.removeElement("Chair") //["Furniture": ["Table", "Bed"], "Food": ["Pancakes"]]
    

提交回复
热议问题