113% ROI on Large Enterprise Crowdsourcing Programs - Forrester TEI™ Get the Study ×

A #Swift Series Part 6 – Tuning and Improving

By wwwtc In Uncategorized

Posted August 7th, 2015

Let’s make some cool changes to our existing random number generator just to get more experience with Xcode. If you want to build upon that original app, you can just work directly within that file, but if you want to keep the app because you like it, then I suggest you duplicate the app project and work in the copy.

This next app will be a glorified version of the original, in a way. Basically, it’s going to be an app that will select a random music artist for you to listen to – pretty much like a less powerful version of iTunes Shuffle that doesn’t allow you to listen to the actual song. It won’t have any connectivity to a music player app – it’s merely just a reference point, which I find useful anyways. I often forget which artists I’ve listened to in the past, so it’s always good to be reminded of what I like.

First things first, let’s add a label at the top of the screen, above where your random number generates (or used to generate, if you’re working on a copy of the original app). Make the text in the label some sort of suggestion for an artist, and style it how you want it to be styled. At this point, depending on where your new label sits, you might want to change the placement of the other elements in your app and then add the proper constraints to make everything look good. If you don’t remember how to set constraints, refer back to my previous blog here. Here’s mine with the new label (note the placeholder text for where the number/artist goes – this will be addressed later):

We can tweak the design later. For now, let’s focus on the artists. You probably already guessed the best way to store the artists – an array. At this point, let’s delve into refactoring our code. Haphazardly designed code is marginally acceptable in simpler programs such as this one, but we want to cultivate good development habits so when we tackle much larger programs in the future, we can avoid confusing ourselves and other developers who look at our work. To do this, let’s add a new Swift file to our project and put this code in that file. Go to the main project folder holding all the files, right click it, and press the “New File…” option. From there, select the Swift option. Name the file something relevant. The new file should pop up under the subfolder titled Supporting Files :

From there on out, let’s create a class to hold everything together, and within that class, an array. In that array, you’ll place the names of artists you want to recommend. Here’s a snapshot of my own code:

Within this class, let’s add a function that generates a random artist. Basically, we’ll be taking the arc4random_uniform function we had previously put in our ViewController.swift file into our new artists.swift file. This is all part of the process of refactoring our code.

This time around, however, we can’t just generate a random number of our choosing – it has to accord to the size of our array. If we have ten artists in our array, but our function generates a random number up to 100, we’ll run into an error, and our program will stop working. Thus, we need to implement a count in our function that monitors the number of artists in our array:

Notice that my variable artistCount converts the count of the array into an unsigned 32-bit integer. This is because arc4random_uniform accepts only that kind of integer. However, once we get the random number, which we store into the variable randomArtist, we have to again convert it, this time to a regular integer, since when we select an artist from artistsArray, we have to use Int, and not UInt32. Finally, notice the brace at line 41 – that denotes the end of the class, in which randomArtist is included.

Back in our ViewController.swift file, we want to create an instance of our data model so we can use it. To do so, use the initializer syntax and write this:

Next, we’ll want to apply the randomArtist method to change the text of numberLabel:

If you’ve done this all correctly and you run your app, you should be able to generate artist names when you press the button. However, we’re not done with this part just yet. Currently, the app doesn’t have an artist appear when it first loads up. You might have some placeholder text there, or perhaps something else that isn’t completely relevant. We want to make our app automatically recommend an artist when we start it up. To do so, let’s take the numberLabel.text assignment we put under our button function into the viewDidLoad function:

Now, if you go back and run your app again, you should automatically get an artist when the app starts up.

Lastly, let’s make some alterations to our text label. Some artists have incredibly long names, like Godspeed You! Black Emperor or Captain Beefheart and his Magic Band. Our label, as it is, might not enough to encapsulate some of these names. To make it more versatile, expand the size of your text box. Once you’re satisfied with the new size of your box, go to the Attributes Inspector and set the Lines option, which should have a value of 1, to 0. Then, go to Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints (under Selected Views). After this, your label should be optimized to handle longer text: