Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.

Swift Tips

posted on July 6, 2021 by Mark Roxberry
main post image
Notes and tips for Swift development

Swift tips

Probably basic stuff, but wanted to keep a reference of useful tricks

Simulator not updating the Launch Image

I added a Background color, ran the simulator, then added a Launch Image and reran the simulator, the image did not show. Just restart the simulator.

Object to JSON String

CustomObject using a data extension, I wanted to get the UUID and Color properties in JSON to reuse in a JSON file

    do {
        let encodedData = try JSONEncoder().encode(CustomObject.data)
        let jsonString = String(data: encodedData,
                            encoding: .utf8)
        print (jsonString ?? "no json")
    } catch {
        fatalError("Couldn't process json")

JSON to CustomModel

    if let jsonData = jsonString?.data(using: .utf8) {
        let objectFromJsonData = try JSONDecoder().decode(CustomObject.self,
                                                    from: jsonData)

Print or NSLog

Use print for quick and dirty, use NSLog for more info.