Hey. I have an array that stores several nsDictionary (there are different numbers of them), it looks like this:

[{ BTmac = "123"; DeviceModel = "BBB"; DeviceType = 1; ID = 1; ST = 3; }, { BTmac = "123"; DeviceModel = "BBB"; DeviceType = 1; ID = 2; ST = 5; }, { BTmac = "123"; DeviceModel = "NNN"; DeviceType = 0; ID = 3; ST = 5; }] 

I need to collect the key ID values ​​from all the dikshins into separate two arrays (there may be different numbers, both 1 and 100), if DeviceType = 1 , then in the first array, if DeviceType = 0 then in the second, what would be like this: first = [1,2] second = [3]

Thank.

  • what you tried, what did not work? - Max Mikheyenko
  • @MaxMikheyenko wrote a for in loop, which is embarrassing to show, I can’t figure out how to recreate the desired structure, or another approach is needed at all .. - pbogdanv

1 answer 1

 let sourceArray = [[ "BTmac" : "123", "DeviceModel" : "BBB", "DeviceType" : "1", "ID" : "1", "ST" : "3" ], [ "BTmac" : "123", "DeviceModel" : "BBB", "DeviceType" : "1", "ID" : "2", "ST" : "5" ], [ "BTmac" : "123", "DeviceModel" : "NNN", "DeviceType" : "0", "ID" : "3", "ST" : "5" ]] var firstArray:Array = [Int]() var secondArray:Array = [Int]() for (_, element) in sourceArray.enumerated() { if(Int(element["DeviceType"]!)! == 1) { firstArray.append(Int(element["ID"]!)!) } else { secondArray.append(Int(element["ID"]!)!) } } print(firstArray) print(secondArray) 
  • Your option works great, the line with append was rewritten in the following way: `firstArray.append (element [" ID "]! As! Int)`, otherwise I swore. I can not understand why dizlaik came to the topic .. - pbogdanv