Password generator class I've written this class to generate a random password and was wondering if there's anything I could improve upon here and whether or not this a safe (most random) method of generating a password.

There's only one function in here which can be used to create an alphanumerical password of a specified length. It can also be overloaded to be more specific in which characters it will use.

The code works as it's supposed to.

import UIKit

class codeGen: NSObject {

    private let base: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    private let lowerChars: String = "abcdefghijklmnopqrstuvwxyz"
    private let numberChars: String = "1234567890"
    private let specialChars: String = "!@#$%^&amp;*"

    func generate(length: Int) -&gt; String
    {
        let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        let len = UInt32(letters.length)

        var result = ""

        for _ in 0 ..&lt; length {
            let rand = arc4random_uniform(len)
            var nextChar = letters.character(at: Int(rand))
            result += NSString(characters: &amp;nextChar, length: 1) as String
        }

        return result
    }

    func generate(length: Int, lowers: Bool, numbers: Bool, specials: Bool) -&gt; String
    {
        var letters = base

        if (lowers == true)
        {
            letters += lowerChars
        }
        if (numbers == true)
        {
            letters += numberChars
        }
        if (specials == true)
        {
            letters += specialChars
        }

        let NSletters : NSString = letters as NSString
        let len = UInt32(NSletters.length)

        var result = ""

        for _ in 0 ..&lt; length {
            let rand = arc4random_uniform(len)
            var nextChar = NSletters.character(at: Int(rand))
            result += NSString(characters: &amp;nextChar, length: 1) as String
        }

        return result
    }

}
Type names in Swift use UpperCamelCase (compare API Design Guidelines), so it should 
be class CodeGen.

I do not see any reason why the class must inherit from NSObject.
Actually the methods do not use any state. If the sole purpose of
the class declaration is to provide a namespace (and to avoid free
global function) then you use an enum with type methods instead:

enum CodeGen {

     static func generate(length: Int) -&gt; String { ... }

}


so that you can call them as

let password = CodeGen.generate(length: 16)


instead of creating an instance:

let password = CodeGen().generate(length: 16)


The advantage of usage an enum over a struct is that you can not
(inadvertently)  create an instance of the type 
(compare https://github.com/raywenderlich/swift-style-guide#constants,
or Swift constants: Struct or Enum).

The explicit type annotations in

private let base: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// ...


are not necessary, that can be simplified to

private let base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// ...


You have two functions implemented independently:

func generate(length: Int) -&gt; String 

func generate(length: Int, lowers: Bool, numbers: Bool, specials: Bool) -&gt; String


but actually the first function is a special case of the second.
Therefore the first method can simply forward to the second:

func generate(length: Int) -&gt; String {
    return generate(length: length, lowers: true, numbers: false, specials: false)
}


Alternatively, implement only a single method with default
parameter values:

func generate(length: Int, lowers: Bool = true, numbers: Bool = false, specials: Bool = false) -&gt; String


Testing a boolean value if (lowers == true) can always be
simplified to if (lowers), and the parentheses are not needed:

if lowers {
    letters += lowerChars
}


The type annotation in

let NSletters : NSString = letters as NSString


is again unnecessary, and variable names should start with a lowercase
letter. But the bridging to NSString can be avoided completely by creating a Swift Array of Character:

let characters = Array(letters.characters)
let numChars = UInt32(characters.count)
var result = ""
for _ in 0 ..&lt; length {
    let rand = Int(arc4random_uniform(numChars))
    let nextChar = characters[rand]
    result.append(nextChar)
}


which can be simplified to

let characters = Array(letters.characters)
let numChars = UInt32(characters.count)
let result = String((0..&lt;length).map { _ in
    characters[Int(arc4random_uniform(numChars))]
})


The argument names can be improved, perhaps useLowercase or
withLowercase instead of lowers etc.

Putting it all together the code would look like this:

enum CodeGen {

    private static let base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    private static let lowerChars = "abcdefghijklmnopqrstuvwxyz"
    private static let numberChars = "1234567890"
    private static let specialChars = "!@#$%^&amp;*"

    static func generatePassword(length: Int, useLowercase: Bool = true,
                                 useNumbers: Bool = false,
                                 useSpecialChars: Bool = false) -&gt; String {
        var letters = base
        if useLowercase {
            letters += lowerChars
        }
        if useNumbers {
            letters += numberChars
        }
        if useSpecialChars {
            letters += specialChars
        }

        let characters = Array(letters.characters)
        let numChars = UInt32(characters.count)
        let result = String((0..&lt;length).map { _ in
            characters[Int(arc4random_uniform(numChars))]
        })

        return result
    }

}


The static variables can also be moved into the method unless
you need to share them with other methods of the CodeGen type.