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

Project 9 | Day 40 | Challenge 2 | Is it good practice to mix performSelector and DispatchQueue?

Forums > 100 Days of Swift

Hello everyone! I hope you're all safe and well.

I am using both performSelector and DispatchQueue to solve Challenge 2 in Project 9 and although it works, I'm not sure if this is the right approch. Any thougths?

Note: Whenever I call loadLevel (in viewDidLoad and levelUp), I use: performSelector(inBackground: #selector(loadLevel), with: nil)

@objc func loadLevel() {
        var clueString = ""
        var solutionsString = ""
        var letterBits = [String]()

        if let levelFileURL = Bundle.main.url(forResource: "level\(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: "")
                    solutionsString += "\(solutionWord.count) letters\n"
                    solutions.append(solutionWord)

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

        DispatchQueue.main.async { [weak self] in
            self?.cluesLabel.text = clueString.trimmingCharacters(in: .whitespacesAndNewlines)
            self?.answersLabel.text = solutionsString.trimmingCharacters(in: .whitespacesAndNewlines)
            debugPrint(self?.answersLabel.text ?? "answersLabel is empty")
            debugPrint("Solutions array = \(self!.solutions)")

            letterBits.shuffle()
            //letterButtons.shuffle()

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

3      

Hi. I didn't try mixing the 2 approaches, but perhaps it could be best to stick to one style for consistency sake. Have fun!

3      

My first inclination was to do it this way as well, seemed to work just fine lol.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.