Maffster
Maffster

Reputation: 25

How do a pass multliple returns from a function to a a structured variable in swift (OSX)

I'm trying to take two return values from one function, and put them into a variable that has a defined structure.

I know I'm not doing it right because I'm getting an error. What is the correct way to do this? - or am I barking up the wrong tree?

struct aWord {
    var letters: [Character] = []
    var word: String = ""
}

let myDictinary = ["cheese", "tree","pea","fleas","house"]

var chosenWord: aWord

func pickWord() -> (letters: Array<Character>, fullWord:String) {
    var x = UInt32(myDictinary.count - 1)
    var n = Int(arc4random_uniform(x))
    var chosen = myDictinary[n]
    var word : [Character] = []

    for letter in chosen {
        word.append(letter)

    }
    return (word, chosen)


}


chosenWord = pickWord()

println(chosenWord.word)

The error message I'm getting is for the line chosenWord = pickWord() : (letters: Array, fullWord: String)' is not convertible to 'aWord'

Upvotes: 0

Views: 44

Answers (1)

Kevin
Kevin

Reputation: 56059

chosenWord is a struct, pickWord returns a tuple. You can't just store a tuple into space meant for a struct. Either declare chosenWord to be a tuple (with the appropriate types) or make pickWord return aWord.

Upvotes: 1

Related Questions