-3

I want to know who have "choix1" in this array.

var arrayVote = ["James: choix1, Julien: choix2, Paul: choix1, Teddy; choix1"]

And i want for result: An array with the name of personnes who have "choix1"

Like that : [James, Paul, Teddy]

Dan Boujenah
  • 51
  • 1
  • 4
  • 1
    Are you sure this is an Array and not a Dictionary? Alternatively, are you sure this isn't an Array containing one element, `"James: choix1, Julien: choix2, Paul: choix1, Teddy; choix1"`? By "are you sure" I mean "have you accidentally miscopied code". – BallpointBen Apr 18 '16 at 09:42
  • Use dictionary in this case is much easier. – Twitter khuong291 Apr 18 '16 at 09:42
  • 1
    If you actually meant a *dictionary* then you'll find solutions here: [Swift dictionary get key for value](http://stackoverflow.com/questions/27218669/swift-dictionary-get-key-for-value). – Martin R Apr 18 '16 at 09:45
  • No i mean Array, my data is already with this Type – Dan Boujenah Apr 18 '16 at 10:22
  • Then you'll have to dissect the string first. That's possible, but cumbersome: what if a *person* has a comma, colon, or "choice" in its name? – Martin R Apr 18 '16 at 10:31

1 Answers1

0

I think you should create [NSDictionary] instead on [String] and do like so:

var arrayVote = ["James": "choix1", "Julien": "choix2", "Paul": "choix1", "Teddy": "choix1"]
var result: [String] = []
arrayVote.forEach {
    if $1 == "choix1" {
        result.append($0)
    }
}
print(result) // ["Paul", "Teddy", "James"]
Emptyless
  • 2,964
  • 3
  • 20
  • 30
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116