-2

I have one NSMutableDictionary with different keys and values. All key values I need to save in NSMutableArray in Swift3.

Here is my NSMutableDictionary :

  {
    92 =     (
        "<Group.VideoRange: 0x6040003a09a0>",
        "<Group.VideoRange: 0x6040003a0c40>"
    );
    93 =     (
        "<Group.VideoRange: 0x6040003a0540>"
    );
  }

Note: Here 92 and 93 is my dictionary keys.

Below is my code to add dictionary all values in my array.

for value in videoRangeDic.allValues{

    arrVideoRange.add(value)
}

As an output, I am getting below array value :

(
        (
        "<Group.VideoRange: 0x6040003a0540>"
    ),
        (
        "<Group.VideoRange: 0x6040003a09a0>",
        "<Group.VideoRange: 0x6040003a0c40>"
    )
)

Instead, I want my output should be like :

    (
        "<Group.VideoRange: 0x6040003a0540>",
        "<Group.VideoRange: 0x6040003a09a0>",
        "<Group.VideoRange: 0x6040003a0c40>"
    )

The reason is dictionary is saving values in array like (), So I just need to remove this array. Can anyone please help me to solve my problem without creating any new array. Thank you!

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
user2786
  • 656
  • 4
  • 13
  • 35

3 Answers3

1

You can get all the value of your dic by single line code

print("values array : \(videoRangeDic.map{$0.value}.flatMap{$0})")
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

If your arrVideoRange is NSMutableArray like this

var arrVideoRange : NSMutableArray = []

then try this

for value in videoRangeDic.allValues{

    arrVideoRange.addObjects(from: value as [Any])

}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
0

You can use flatMap to flatten the array of dictionaries to array

let array = groupDicts.flatMap { $0.values.joined() }

or

let newArray = Array(dicts.lazy.flatMap { $0.values }.joined())
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60