UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Code review for challenge 3 of original 100 days

Forums > 100 Days of Swift

Greetings everybody

Here was the challenge we were given. This comes from a milestone challenge to create a hangman game.

Here is my solution.

I've never written much UIKit code and this is my first time through the original course.

I wrote the entire thing using code -- no storyboard. It took me several days and many bugs to fix to get it to work right. The UI isn't pretty and it has no drawing of a noose or body. I haven't learned how to draw in UIKit yet.

It's long (about 450 lines of code) but a lot of it is just setting up labels and constraints so that can mostly be ignored. The main parts are the functions I wrote. There's also a file for a String extension.

I was hoping people could look at my code and give me any advice/ criticism on how I implemented this and if there were better ways of doing things.

Thanks so much, Rob


I like that you took on a bigger challenge!

Sadly, I started looking at it on a device that wasn't sized right to finish the game. But it all worked. So!

The amount of code will be somewhat less overwhelming if you add a function to initialize the labels, e.g.:

    func makeLabel(text: String, font: UIFont, textColor: UIColor) -> UILabel {
        var label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = text
        label.font = font
        label.textColor = textColor

        return label

…and call it like so:

        currentWordLabel = makeLabel(text: currentWord,
                                font: UIFont.systemFont(ofSize: 24),
                                textColor: UIColor.systemGray)

…which will reduce a lot of the wall of labels at the beginning.

The general way of solving the layout challenge is to get the size of the screen (UIScreen.main.bounds), decided on left/right and/or top/bottom measurements, then dividing the remaining space between the number of items you want to show. While 3x9 is useful for the taller, skinnier iPhones, 4x7 may work better for squarer devices.

As far as centering (an implied question in a comment in your code), the previous paragraph can work. Out in industry most people using code for layout are using autolayout, which has a pretty descriptive layout syntax. The guys over at talk.objc.io did several free episodes (241 is the one I'm thinking of) showing how autolayout can solve some problems that are (or at least were at the time of the video) harder to implement in SwiftUI.

FYI: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/index.html

(Autolayout can be expressed either in storyboards or in code.)

You are getting some constraint conflicts in the console window, but that's a polish thing.

Solid effort!


Thanks so much @deirdresm for looking at my code and the suggestions. I'm only on day 44 so I still have a long way to go. I'm sure @twostraws will be addressing a lot of this. Rob


Weirdly, I don't know how much he teaches about auto layout as I was in the middle of the UIKit course when the SwiftUI one came out, so I switched over to that. I keep meaning to get back to the UIKit one and do both the app as written plus a SwiftUI version, just…there's always something else to do. (Also, I'm mostly a macOS programmer, so the UIKit one is less relevant to my job.)


Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.