Another RandomPassword Question


#1

Xcode 7.2 did not like this code from the book:

private let characters = Array( “0123456789abcdefghijklmnopqrstuvwxyz” + “ABCDEFGHIJKLMNOPQRSTUVWXYZ”)

and Xcode changed it to this from the ‘Fix it’ option:

private let characters = Array(arrayLiteral: “0123456789abcdefghijklmnopqrstuvwxyz” + “ABCDEFGHIJKLMNOPQRSTUVWXYZ”)

However, now I get an error here:

return character

because Xcode thinks character is a string.

Here is all of my code in GeneratePassword.swift:

import Foundation

private let characters = Array(arrayLiteral: “0123456789abcdefghijklmnopqrstuvwxyz” + “ABCDEFGHIJKLMNOPQRSTUVWXYZ”)
func generateRandomString(length: Int)->String{

//start with an empty string
var string=""

//Append 'length' number of random characters
for index in 0..<length{           //warning here that length is never used
	string.append(generateRandomCharacter())

}
return string

}

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

//get and return a random character
let character =  characters[index]
return character  //error here, Xcode thinks character is a string

}

How can I correct the code to eliminate the errors?

Thank you…
Earl Staley


#2

See my answers here: http://forums.bignerdranch.com/viewtopic.php?f=513&t=10676#p30585


#3

Thank you for your reply.

Now I get an error at:

let character = characters[index] //error: Cannot subscript a value of ‘String.CharactrerView’ with an index of type ‘int’.

How can I fix this error?

Here is my revised code:

import Foundation

private let characters = Array((“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”).characters)

func generateRandomString(length: Int)->String{

//start with an empty string
var string=""

//Append 'length' number of random characters
for index in 0..<length{    //warning here that length is never used
	string.append(generateRandomCharacter())

}
return string

}

func generateRandomCharacter()->Character{

//create a random index into the character array
let index = Int(arc4random_uniform(UInt32(characters.count)))

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

}

Additionally, the responder, in the link you provided, stated:

“I’ve been using the companion guide but I overlooked that somehow. Glad it was an easy fix.”.

Is there a companion guide to the Cocoa book, and if so, where can I obtain it?

Thank you…
Earl Staley


#4

Oh, so you didn’t see this: http://forums.bignerdranch.com/viewtopic.php?f=512&t=10425

Well sadly the book is not very valuable in its current state as the authors have all but abandoned it it seems.
Most of its code is obsolete as it uses Swift 1.2 syntax.

Instead of updating the book (which is the whole point of electronic distribution) they provided a text file for errata, and one for Swift 2.0 changes which is a complete pain to read through in parallel to the book. (You basically have to check those two files constantly as there are plenty of errors and changes)
You will find both files through the link above.


#5

Ignore my last post. I found the answer to both questions.


#6

I went to your link but do not see the code