Swift Tips
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(objectFromJsonData.id)
}
Print or NSLog
Use print for quick and dirty, use NSLog for more info.
This post and/or images used in it may have been created or enhanced using generative AI tools for clarity and organization. However, all ideas, technical work, solutions, integrations, and other aspects described here are entirely my own.