I'm experimenting with dictionaries in Swift 4. I'm trying to make an array from a couple of values belonging to a randomly picked key within a dictionary. I'm not exactly sure how I need to do this in Swift 4.
//: Dictionary Test
import UIKit
var database = ["Albert Einstein": ["Alberts first quote",
"Alberts second quote",
"Alberts third quote"],
"Martin Luther King": ["Martin's first quote",
"Martin's second quote",
"Martin's third quote"],
"Newton": ["Newton's first quote",
"Newton's second quote",
"Newton's third quote"]]
func randomQuote(){
//Make an array from database keys
var authorArray = Array(database.keys)
//Pick a random author
let author = (authorArray[Int(arc4random_uniform(UInt32(authorArray.count)))])
//Make an array from values based on the author we've picked (HERE'S THE PROBLEM)
let quoteArray = Array(database[author].values)
//Pick a random quote from the choses author
let quote = (quoteArray[Int(arc4random_uniform(UInt32(quoteArray.count)))])
print(author)
print(quote)
}
randomQuote()
Now obviously let quoteArray = Array(database[author].values)
is not working. Anyone an idea how this would work?
Aucun commentaire:
Enregistrer un commentaire