GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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+

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!

Learn more here

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.