Heinevolder
Heinevolder

Reputation: 338

How to return random dictionary

Is it possible to return a dictionary this way? i guess it's just some syntax i got wrong.

struct Dare {
  var theDare: [String: String, String: Bool;] = [
    ["dare": "Dare1",
     "darePerson": true],
    ["dare": "Dare2",
     "darePerson": false],
    ["dare": "Dare3",
     "darePerson": false],
    ["dare": "Dare4",
     "darePerson": true],
    ["dare": "Dare5",
     "darePerson": false]
]

func randomDare() -> Dictionary<String, Bool> {
    return theDare[Int(arc4random_uniform(UInt32(quotesArray.count)))]
}

}

Upvotes: 0

Views: 41

Answers (1)

The Tom
The Tom

Reputation: 2810

Your list of dictionaries is not declared correctly. Instead of being :

var theDare: [String: String, String: Bool;]

It should be :

var theDare: [[String: AnyObject]]

as you always have String keys but sometimes have String values and sometimes Bool values.

Your randomDare() function return needs to be changed accordingly to :

func randomDare() -> Dictionary<String, AnyObject>

Upvotes: 2

Related Questions