Swift tutorial for beginners Part 3

Fabio Rocha
Feb 23, 2017 · 4 min read

First of all, THANK YOU! Thank you for waiting, and I’m sorry for the time I’ve been away, lots of great things happened on my professional career and I was not expecting so many readers. That being said, lets proceed to our 3rd part of this tutorial.

Time passed by that we are now on Swift 3! Fortunately enough Xcode now has an automated feature to migrate the code to the new syntax, but don’t fool yourself, it wont work every time and you should validate the changes manually.

Xcode Swift automated migration

Also there were some other changes and while migrating this project I ran into an issue having a black colored margin on the top and bottom of my storyboard when building on the simulator, the solution is quite simple and you just need to create a Launch Screen and set a “Launch Screen File” as follows:

Creating Launch Screen
Setting a Launch Screen Storyboard on Xcode

And we should be ready to continue! In this tutorial I’ll be creating a password generator, something really simple, but we have to start somewhere! Also, I will be using TDD (Test Driven Development), the market is in high demand for developers, but now more than ever the market wants tested quality software, so you would be wise to make yourself comfortable with Unit and UI testing.

Let’s create a class responsible for generating passwords only, I will call it PasswordGenerator.swift and the test class PasswordGeneratorTests.swift.

File tree

Again, there might be different approaches at this point, personally I prefer to create the interface first so I can write the tests without having the compiler crashing. A purist would probably write the tests without the interface.

My PasswordGenerator starts as follows, notice the typealias where you can define a type, instead of repeating its definition multiple times you can just use a more descriptive name.

Password Generator Interface

Then, two very simple unit tests, notice how the 2nd one succeeds as a result of my interface definition before, returning an empty password passes as a valid characters password, in this case my 1st test makes sure this edge case is caught.

Password Generator Tests

So now I have two scenarios tested, the password length and the password characters used. The next step is to create code and turn this reds into greens. Make sure you run the tests at every change and before every commit to make sure nothing is broken.

Notice that I have fixed the typo with the lenght/length :) And obviously it doesn’t affect the tests as the behavior is the same.

Now we are storing the characters, each Password generator works with a single Character Set since it’s initialization.
The generation is simply a loop picking random digits from the Characters Set, notice how you can’t simply use “[N]”, we need to create a valid index and this applies to Strings also.

I’ve used the underscore in two different scenarios, the method definition and the for:

  • Underscore on the method definition:
    - The IDE will ignore your parameter name and instead of writing generate(length: 10), you can write generate(10)
  • Underscore on the for loop:
    - It works as a discarded value token, if I would replace the token by i, the IDE would warn me that i is not being used.

Our PasswordGenerator class is done, and the most important parts are being tested, still, some more tests can be added.


This was meant to be the final part, but it would be too much text, so for now that’s all! In the next part we will be finishing the UI!

I would also love to introduce you to Swift Generics, maybe on part5!

You can access the source files for this tutorial here!

I promise I wont take 2 years to write the next one!

Share your love and drop me a tweet @fabiojgrocha

Fabio Rocha

Written by

Hello, I’m Fabio, an iOS developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade