Skip to content

Instantly share code, notes, and snippets.

View andreabusi's full-sized avatar

Andrea andreabusi

View GitHub Profile
@andreabusi
andreabusi / bubidevs-swiftui-picker.swift
Last active March 30, 2023 10:05
How to manage a nullable selection with SwiftUI Picker.
import SwiftUI
struct Car: Identifiable, Hashable, Equatable {
let id = UUID()
let model: String
let team: String
static func cars() -> [Self] {
[
.init(model: "SF23", team: "Ferrari"),
@andreabusi
andreabusi / swift-dictionary-prettyprint.swift
Last active June 7, 2022 14:46
Swift - Convert JSON dictionary into a pretty printed string
public extension Dictionary {
/// Returns a pretty printed JSON version of the current dictionary.
/// String uses white space and indentation to make the resulting data more readable.
var prettyPrintedJson: String {
do {
let data = try JSONSerialization.data(withJSONObject: self, options: [.withoutEscapingSlashes, .prettyPrinted])
return String(data: data, encoding: .utf8) ?? ""
} catch {
return ""
}