Random Password Generator Fixes for Xcode 8.1 and Swift 3

I am a novice and I had a lot of trouble getting the code for the Random Password Generator to work. Here are the changes I made to the code to get the app to work. As I said, I am a novice so some of these changes may not have been necessary. But they did stop the error codes lead to a successful build.

GeneratePassword.swift file

I rewrote the characters declaration to layout the numbers and letters in “A”, “B”, … array format. That seemed to solve a lot of problems. (I didn’t put all the characters in):

private let characters = Array (arrayLiteral: “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”)

In the generateRandomString function I replaced index it the for statement with an underscore. Xcode didn’t like variable being declared then not used elsewhere in the function.

for _ in 0…<length {

In the generateRandomCharacter function, I replaced the return type “Character” with “String”. I don’t know why Character wouldn’t work.

func generateRandomCharacter() -> String {
// Create a random index into the characters array
let index = Int(arc4random_uniform(UInt32(characters.count)))

MainWindowController.swift file

In the @IBAction statement, I added “length:” to the parameter for the generateRandomString function. I have no idea why this worked, but it did.

@IBAction func generatePassword(sender: AnyObject) {
    // Get a random string of length 8
    let length = 8
    let password = generateRandomString(length: length)

I believe these are all the changes I made


It would have made more sense if you had just pasted the code you used, but thanks for sharing.

I needed to update only …

let password = generateRandomString(length: length)

… in @IBAction func generatePassword

Xcode 10.2.1 / Swift 5.0.1