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 String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.