Tuesday, February 27, 2024

Learning Swift pt 1. (of who knows how many posts)

 I came across some old code I had in a project and I can't understand what is happening:

        var str = "blah"

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in

            print(response)

            var err: NSError?

            do {

                let myJson = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) //as [[String:Any]]

                print(myJson)

                str = String(describing: myJson)


                Task {

                    await MainActor.run {() -> Void in

                        self.m_textView.string = str

                    }}

                

            } catch let error as NSError {

                err = error

            }

        }.resume()


So how to parse this? Because at the end of the block we see the resume() being called. Does the resume() return a Task object? No. I just remembered that a block at the end is actually passed as the last parameter to the function being called (dataTask()). So what returns a task object is the call to dataTask() not the call to resume(). This syntactic sugar can be confusing!


But on second thought resume() is only called on a Task() object right? So is the .resume() actually being called on the result of "let task ="? Ugh.

This is one of the reasons I hate swift. What code does is not obvious.

Ok, I found some sample code on Apple's site which is clearer:

func startLoad() {

    let url = URL(string: "https://www.example.com/")!

    let task = URLSession.shared.dataTask(with: url) { data, response, error in

        if let error = error {

            self.handleClientError(error)

            return

        }

        guard let httpResponse = response as? HTTPURLResponse,

            (200...299).contains(httpResponse.statusCode) else {

            self.handleServerError(response)

            return

        }

        if let mimeType = httpResponse.mimeType, mimeType == "text/html",

            let data = data,

            let string = String(data: data, encoding: .utf8) {

            DispatchQueue.main.async {

                self.webView.loadHTMLString(string, baseURL: url)

            }

        }

    }

    task.resume()

}


Here you see that resume is actually being called on the task. Ugh.

Update: Mystery solved! That first piece of code is wrong. The calculated type of task would be void because resume() returns void. I copy-pasted that code from somewhere and that somewhere has bad code. Ugh.

Friday, February 20, 2009

Mythic Map Pack - Whoop-De-Doo!

So much fuss about three new maps!
I want some of my cherished Halo 2 maps to return!
How much work can it be to port those over?
I've spent so many hours on those Halo 2 maps and I want to spend more time on them while playing Halo 3.  It would be cool to record some films of games on those maps.

Monday, February 9, 2009

Some Reasons I Hate Bungie

GRRRR!

1. No bots in Halo 3. I want some better way of training than just playing on XBox Live. Boy is it frustrating practicing a certain move (perfecting sniping, for example) with everyone shooting at you!

2. No maps from Halo or Halo 2 (except for Lockout/Blackout).

3. Map creation tools are crappy. I want something better than stacking boxes!

4. Huge delay when displaying the first game choice in multiplayer. Many times the first game choice will be displayed after a long delay only to be vetoed in the lobby. What's with the long delay? Is it loading a map? Maybe they should just display the thumbnail and not load the map.

Test