2

I have a JSON object as such:

The Result {
    shops = ({
        date = "2015-07-22T14:14:48.867618";
        "num_items" = 0;
        score = "-1";
        "total_spend" = 0;
    },
    {
        date = "2015-07-22T14:17:03.713194";
        "num_items" = 1;
        score = 5;
        "total_spend" = "1.85";
    });
}

And My Code:

let dict = result as! NSDictionary
var mutDict: NSMutableDictionary = dict.mutableCopy() as!     NSMutableDictionary

for (key, value) in mutDict {
    for i in 0..<backendArray.count {
        if value[i].objectForKey("num_items") === 0
            {
                //delete that shop only
            }
     }       
}

I want to delete the whole first shop and all of it's contents, but not the second shop or the shops.

I don't want to turn it into an Array, because I can't figure out how to turn it back. Data needs to be stored still as a Dictionary. Thanks

HannahCarney
  • 3,441
  • 2
  • 26
  • 32

2 Answers2

2

The key in the dictionary is shops, and it has an array as value. So you are not actually deleting the shops with num_items == 0 from the dictionary but from the array (which is probably not mutable). So create a new array by filtering the old one, and update the value for shops in dictionary, e.g.:

dict["shops"] = (dict["shops"] as NSArray?)?.filter { (shop: Dictionary) in
    (shop["num_items"] as NSNumber?)?.integerValue > 0
}

(Verify type checks and casts as necessary; the code you gave is not stand-alone so can't test directly in a playground.)

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • I read the OP's post differently. I thing she's saying that she wants to delete any (outer) dictionary entry that contains at least one zero entry in its array. We need the OP to clarify. – Duncan C Jul 24 '15 at 15:43
  • Doesn't work :( I want to delete the whole shop it says something about AnyObject not having method filter – HannahCarney Jul 24 '15 at 17:00
  • 2
    @HannahLouisaCarney Well, that's the problem with not posting testable code… Try the edited version which adds the `as NSArray`. If that doesn't work, try changing `NSArray` to `Array`. If that doesn't work, post _details_ as to how it doesn't work, i.e., copypaste the exact error message, not “it says something about”. – Arkku Jul 24 '15 at 18:27
2

So use the NSMutableDictionary method removeObjectForKey. You will need to break out of your inner for loop when you delete an entry however.

let dict = result as! NSDictionary
var mutDict: NSMutableDictionary = dict.mutableCopy() as!     NSMutableDictionary

dictLoop: for (key, value) in mutDict {
    for i in 0..<backendArray.count {
        if value[i].objectForKey("num_items") === 0
            {
              //delete that shop only
              mutDict.removeObjectForKey(key)
              continue dictLoop
            }
     }       
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 2
    What does "Doesn't work" mean, specifically? Doesn't compile? Compiles but doesn't give the desired result? Crashes? Give details. – Duncan C Jul 24 '15 at 17:30