WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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)
                }
            }
        }
    }

1      

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!

1      

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

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out 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.