0
votes

I have two Dictionaries that look like:

var dict1 = [Int: [String: AnyObject]]

var dict2 = [Int: [String: AnyObject]]

for example:

dict1: [0: ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"], [1: ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"]

dict2: [0: ["sender": "user2", "time": NSDate(), "mId": "J2fAS92D"], [1: ["sender": "user2", "time": NSDate(), "mId": "Sjf82FsJ"]

I want to find out if the dictionaries have data with the same mId and if, I want to do some action. I wrote this code for that and got these errors.

for (_, value) in dict1! {
  for content in value {
    if content.0 == "mId" {
      var mID = content.1
      for (_, 2ndValue) in dict2 {
        for 2ndContent in 2ndValue {
          if 2ndContent.0 == "mId" {
            var 2ndMID = 2ndContent.1
            if mID == 2ndMID {
              //Do Some Action
            }
          }
        }
      }
    }
  }
}

Here are the errors: https://www.dropbox.com/s/e28ple6640hkrzo/Bildschirmfoto%202015-08-28%20um%2021.21.54.png?dl=0

2
There are lots of syntax errors, and I'm not sure where things like arr are coming from. That being said, your immediate errors are because you can't begin a variable name with a number.Eric
Oh - normally I wouldn't to that, but I thought, if a var could be a smiley, it could start with a number as wellPhilHarmonie

2 Answers

1
votes

Firstly, as Eric says in his comment, you are getting errors because you can't start a variable name with a number.

Secondly what you are calling a 'dictionary' here is in fact an array of tuples - if you define your dict1 variable in the Swift REPL by running var dict1 = [Int,[String,AnyObject]()) you get: dict1: [(Int, [String : AnyObject])] = 0 values

A dictionary with the equivalent structure would be defined thus: var dict1 = [Int:[String:AnyObject]]() - this returns dict2: [Int : [String : AnyObject]] = 0 key/value pairs in the REPL.

Therefore the dict1 and dict2 declarations you show will not work. They need to be declared as follows to match the type you've specified:

var dict1 = [(0, ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"]), (1, ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"])]

var dict2 = [(0, ["sender": "user2", "time": NSDate(), "mId": "J2fAS92D"]), (1, ["sender": "user2", "time": NSDate(), "mId": "Sjf82FsJ"])]

If you want to use an actual dictionary, use :

var dict3 = [[0: ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"]], [1: ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"]]]

While I can see what you're trying to achieve in the code you've posted, I don't think the approach you're taking is the right one. Any time you have 4 levels of nested for loops you should consider whether there's a better way of solving the problem. You are iterating over all the keys in the dictionary when you are only interested in the value of "mId". Just looking for the value of that key gets rid of 2 loops and a lot of pointless iterating over things you aren't interested in:

for (_,value1) in dict1 {
  if let mld1 = value1["mId"] {
    for(_,value2) in dict2 {
      if let mld2 = value2["mId"] {
        if mld1 == mld2 {
          println("Found diff")
        }
      }
    }
  }
}  

The if let is necessary to ensure you are making a valid comparison (i.e. to avoid a false positive if the value of 'mld' is nil in both dicts). No idea whether this is even possible in your data model but always worth being thorough. Note that this solution uses arrays of tuples, as in your examples - you'd need something slightly different if you use actual dictionaries.

1
votes

Well im not sure i understood your problem, but this is my idea for that:

for (key, value) in dicOne {
  for (keyTwo, valueTwo) in dicTwo {
    for (keyItem,valueItem) in value {
        for (keyItemTwo, valueItemTwo) in valueTwo {
            if keyItem == keyItemTwo && valueItem == valueItemTwo{
                println("do some action")
            }
        }
    }
  }
}

The first loop is for the first dictionary and the second, for the other dictionary. After this you'r in the second layer ["a": "A"] of both dictionaries. So the next step is to get this dictionary. For this are the other two loops.

Just a question: Did u tried tuple? Like this:

var dictionaryOne:[Int: (String, String)] = [1: ("a", "A")]

var dictionaryTwo:[Int: (String, String)] = [1: ("a", "A")]

for (key, value) in dictionaryOne {
    for (keyTwo, valueTwo) in dictionaryTwo {
        if value.0 == valueTwo.0 && value.1 == valueTwo.1 {
            println("nice action")
        }
    }
}