Sobes.tech
Junior

Con quali strumenti o metodi è possibile eseguire la conversione dei dati?

sobes.tech AI

Risposta dell'AI

  • Codable (con JSONEncoder/JSONDecoder per JSON).
  • Parsing manuale (gestione di Dizionari e Array).
  • Librerie esterne (ad esempio, ObjectMapper, SwiftyJSON).
  • NSCoding (per salvare/ripristinare oggetti nel filesystem).
  • Trasformatori personalizzati per tipi di dati specifici.
// Esempio Codable
struct User: Codable {
    let name: String
    let age: Int
}

let jsonData = Data("{\"name\":\"Alex\",\"age\":30}".utf8)
let user = try? JSONDecoder().decode(User.self, from: jsonData)
// Esempio di parsing manuale
let jsonDict: [String: Any] = ["name": "Boris", "age": 25]
if let name = jsonDict["name"] as? String,
   let age = jsonDict["age"] as? Int {
    // Utilizzo di name e age
}