TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Project 9: Challenge 2 GCD

Forums > 100 Days of Swift

Hi, everyone! I'm trying to do challanges that are part of Project 9. I'm using DispatchQueue.main inside DispatchQueue.global(qos: .background) I'm still not sure if these are the right approach, any thoughts? Thanks in advance!

func loadLevel() {
        var clueString = ""
        var solutionString = ""
        var letterBits = [String]()

        DispatchQueue.global(qos: .background).async {
            if let levelFileURL = Bundle.main.url(forResource: "level\(self.level)", withExtension: "txt"){
                if let levelContents = try? String(contentsOf: levelFileURL) {
                    var lines = levelContents.components(separatedBy: "\n")
                    lines.shuffle()

                    for (index, line) in lines.enumerated() {
                        let parts = line.components(separatedBy: ":")
                        let answer = parts[0]
                        let clue = parts[1]

                        clueString += "\(index + 1). \(clue)\n"

                        let solutionWord = answer.replacingOccurrences(of: "|", with: "")
                        solutionString += "\(solutionWord.count) letters\n"
                        self.solutions.append(solutionWord)

                        let bits = answer.components(separatedBy: "|")
                        letterBits += bits
                    }
                }
            }

            DispatchQueue.main.async {
                //Configure the buttons and labels
                self.cluesLabel.text = clueString.trimmingCharacters(in: .whitespacesAndNewlines)
                self.answerLabel.text = solutionString.trimmingCharacters(in: .whitespacesAndNewlines)

                letterBits.shuffle()

                if letterBits.count == self.letterButtons.count {
                    for i in 0 ..< self.letterButtons.count {
                        self.letterButtons[i].setTitle(letterBits[i], for: .normal)
                    }
                }
            }
        }
    }

3      

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!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.