Broken code on page 23

[code]func generateRandomCharacter() -> Character {
// create a random index into the characters array
let index = Int(arc4random_uniform(UInt32(characters.characters.count)))

// Get and return a random character
let character = characters[index]
return character


The “let character = characters[index]” line gives an error saying “subscript is unavailable: cannot subscript string with an Int…” but I can’t seem to figure out what to replace “index” with…

You have a characters too many in your code.

It should be let index = Int(arc4random_uniform(UInt32(characters.count)))

The additional “characters” on that line actually fixed one of the errors I was getting. I had 2 errors and that was a fix for one of them.

Then you most likely fixed the wrong error of those two.

You have most likely missed the .characters when initializing your array of characters:

This is stated in the file in the GitHub repository of the book which is supposed to help adapt the code to Swift 2.

Ah! Thank you. I’ve been using the companion guide but I overlooked that somehow. Glad it was an easy fix.