Ich habe die folgende Frage, ich generierte Strukturen zum Speichern von Informationen aus JSON im Web. Hier ist der Code-Extrakt, um die Struktur zu verstehen.Verwenden Sie die Struktur mit Arrays (mehrere Ebenen)
struct Planificacion {
let semana: [Semana]
init(semana: [Semana]) {
self.semana = semana
}
struct Semana {
let id: Int
let numero: Int
let clase: [Clase]
init(id: Int, numero: Int, clase: [Clase]) {
self.id = id
self.numero = numero
self.clase = clase
}
struct Clase {
let clase: String
let id: Int
let numero: Int
let fecha_progr: String
let comentarios: [Comentarios]
let actividades: [Actividades]
init(clase: String, id: Int, numero: Int, fecha_progr: String, comentarios: [Comentarios], actividades: [Actividades]) {
self.clase = clase
self.id = id
self.numero = numero
self.fecha_progr = fecha_progr
self.comentarios = comentarios
self.actividades = actividades
}
struct Comentarios {
let id: Int
let texto: String
let fecha: String
let usuario: String
init(id: Int, texto: String, fecha: String, usuario: String) {
self.id = id
self.texto = texto
self.fecha = fecha
self.usuario = usuario
}
}
struct Actividades {
let id: Int
let texto: String
let tipo: String
let ra: [RA]
init(id: Int, texto: String, tipo: String, ra: [RA]) {
self.id = id
self.texto = texto
self.tipo = tipo
self.ra = ra
}
struct RA {
let id: Int
let nombre: String
init(id: Int, nombre: String) {
self.id = id
self.nombre = nombre
}
}
}
}
}
}
var plani = [Planificacion]()
let url = URL(string: "https://www.example.com")!
URLSession.shared.dataTask(with:url) { (data, response, error) in
if error != nil {
print(error!)
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! NSDictionary
for (key, value) in parsedData {
var semanaT = String()
var numeroT = Int()
var idT = Int()
var clasesT = [Any]()
if let semana = parsedData[key] as? NSDictionary {
.
.
.
if let clases = semana["clases"] as? [String:Any] {
for (key, value) in clases {
var claseT = String()
var idClaseT = Int()
var numeroClaseT = Int()
var fecha_progT = String()
var estadosT = [Any]()
var comentariosT = [Any]()
var actividadesT = [Any]()
if let clase = clases[key] as? NSDictionary {
.
.
.
if let actividades = clase["actividades"] as? [String:Any] {
for (key, value) in actividades {
var idActividadT = Int()
var textoActividadT = String()
var tipoActividadT = String()
var rasT = [Any]()
if let actividadesInfo = actividades[key] as? NSDictionary {
.
.
.
if let ras = actividadesInfo["ra"] as? [String:Any] {
for (key, value) in ras {
.
.
.
var RA = Planificacion.Semana.Clase.Actividades.RA(id: idRAT, nombre: nombreRAT)
rasT.append(RA)
}
}
}
**var actividad = Planificacion.Semana.Clase.Actividades(id: idActividadT, texto: textoActividadT, tipo: tipoActividadT, ra: rasT)**
actividadesT.append(actividad)
}
}
.
.
.
}
**var clase = Planificacion.Semana.Clase(clase: claseT, id: idClaseT, numero: numeroClaseT, fecha_progr: fecha_progT, comentarios: comentariosT, estado: estadosT, actividades: actividadesT)**
}
}
}
} catch let error as NSError {
print(error)
}
}
}.resume()
Mein Problem ist jetzt, dass ich nicht die Variable „actividad“ speichern kann, die in fett ist, was ich denke, geschieht durch den „rast“ Array, aber ich bin mir nicht sicher. Die Konsole wirft den folgenden Fehler auf.
error: cannot invoke value of type 'Planificacion.Semana.Clase.Actividades.Type' with argument list '(id: Int, texto: String, tipo: String, ra: [Any])'
var actividad = Planificacion.Semana.Clase.Actividades(id: idActividadT, texto: textoActividadT, tipo: tipoActividadT, ra: rasT)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
Ich kann nicht das Problem finden, die in „clase“ repliziert wird, das fast am Ende des Codes ist, ebenfalls fett.
Kommentar zu Ihrem Codierungsstil; Ich bin Holländer, aber meine Variablen sind alle Englisch. Ich denke, das ist eine gute Übung, da die Lingua Franca der Softwareentwicklung wirklich Englisch ist. –