why I can't access my 3rd level CoreData data in swift?

Trainee Programmer

I have this model:

ここに画像の説明を入力してください

And In one of my ViewController classes I created a method with 3 do while loops (a nested loop) where I insert all the "cursos", "temas" and "subtemas" to populate all my coreData model.

So, inside that method I put this loop to access a part of my coreData data to see if my nested loop work, I did it like this:

    var i = cursos.count
    var index = 0


    do{

        println(cursos[index].nombre)
        println(cursos[index].temas[0].nombre)
        println(cursos[index].temas[0].subTemas[0].nombre)

        index++
    }while(index < i)

"cursos" is my NSManaged object array containing all my "cursos", so in that last loop I think I access the "subTema" data of my first "curso" with the first "tema".

Well, everything went like I want, but When I tried to access the same data in another class (my original goal: have access to my data anywhere), my method to access the data don't recognised my "subTemas" stack of NSManagedObjects linked to my other two stacks of NSManagedObjects. Example:

ここに画像の説明を入力してください

Maybe I need to access my data in another way, through my relationships perhaps? if that so, how can I do it? I really need your help, thanks !

Update: My NSManagedObjects Subclasses generated by Xcode:

Curso.swift

 import Foundation
 import CoreData

 class Curso: NSManagedObject {

 @NSManaged var msjBienvenida: String
 @NSManaged var nombre: String
 @NSManaged var nombrePng: String
 @NSManaged var temas: NSOrderedSet

    }

Tema.swift

  import Foundation
  import CoreData

  class Tema: NSManagedObject {

  @NSManaged var nombre: String
  @NSManaged var curso: Curso
  @NSManaged var subTemas: NSOrderedSet

  }

SubTema.swift

 import Foundation
 import CoreData

 class SubTema: NSManagedObject {

 @NSManaged var nombre: String
 @NSManaged var tema: Tema

 }

Update 2 (relevant code for MartinR):

Inside the VC where I will make a web service call to fill my coreData data, here I don't have to cast my arrays, I can access my data like cursos[index].temas[0].subtemas[ 1 ].name , why ? :

  func webServiceCall2(index:Int){


   var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

   var exeWebServiceCall2 = defaults.objectForKey("exeWebServiceCall2") as? Bool

    if (exeWebServiceCall2 == true){

    var temasArray:[String] = ["Word","PowerPoint","Excel"]
    var subTemasArray: [String] = ["Introduccion","Tema 1","Tema 2"," Tema 3"]

    //Guardar temas y subtemas adquiridos de mi web service a mi grafo de objetos de coreData
    var cantidadCursos = cursos.count
    var indexCursos = 0
    var cantidadTemas = temasArray.count
    var indexTemas = 0
    var cantidadSubTemas = subTemasArray.count
    var indexSubTemas = 0


      var error: NSError?

    do{//para cada uno de mis cursos
        var cursoActual = cursos[indexCursos]

        do{//agregar un objeto entity Tema(que contiene un arreglo con los temas)

        let temaEntity = NSEntityDescription.entityForName("Tema", inManagedObjectContext: managedContext)
        let temaActual = Tema(entity: temaEntity!, insertIntoManagedObjectContext: managedContext)
        temaActual.nombre = temasArray[indexTemas]

            //Inserto todos los temas a mi curso Actual
            var temas = cursoActual.temas.mutableCopy() as NSMutableOrderedSet
            temas.addObject(temaActual)
            cursoActual.temas = temas.copy() as NSOrderedSet

                 do{

                let subTemaEntity = NSEntityDescription.entityForName("SubTema", inManagedObjectContext: managedContext)
                let subTemaActual = Tema(entity: subTemaEntity!, insertIntoManagedObjectContext: managedContext)
                subTemaActual.nombre = subTemasArray[indexSubTemas]

                //Inserto todos los subTemas a mi tema Actual
                var subTemas = temaActual.subTemas.mutableCopy() as NSMutableOrderedSet
                subTemas.addObject(subTemaActual)
                temaActual.subTemas = subTemas.copy() as NSOrderedSet

                    //Guardar, aunque no estoy seguro si puedo usar el mismo managedContext que ya tenia
                    if !managedContext.save(&error)
                      {
                            println("No pude guardar: \(error)")
                      }

                       indexSubTemas++
                    }while(indexSubTemas < cantidadSubTemas)

               //reset index subtemas  y aumento indexTemas para el siguiente ciclo de temas
              indexSubTemas = 0
             indexTemas++
           }while(indexTemas < cantidadTemas)
        //reset index temas  y aumento indexCursos para el siguiente ciclo de cursos
        indexTemas = 0
        indexCursos++
    }while(indexCursos < cantidadCursos)


        defaults.setBool(false, forKey: "exeWebServiceCall2")
        defaults.synchronize()


    }


}
Martin R

temasプロパティがありNSOrderedSet、添字は、[0]型を返しますAnyObjectしたがって(コメントですでに述べたように)AnyObject実際のタイプにキャストする必要があります

let curso = cursos[index]
println(curso.nombre)
let firstTema = curso.temas[0] as Tema
println(firstTema.nombre)
let firstSubTema = firstTema.subTemas[0] as SubTema
println(firstSubTema.nombre)

以下を使用してループを単純化できることに注意してくださいfor - in

for curso in cursos {
    // ...
}

すべてのオブジェクトを再帰的に列挙すると、次のようになります。

for curso in cursos {
    println(curso.nombre)

    for tema in curso.temas.array as [Tema] {
        println(tema.nombre)

        for subtema in tema.subTemas.array as [SubTema] {
            println(subtema.nombre)
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why can't I access my objects member variable?

分類Dev

Why can't I access my JavaScript array by index outside of the d3 then function?

分類Dev

Why can't I access data in parent with jquery?

分類Dev

How can I login a client to a 3rd party site with basic access authentication from django

分類Dev

Why can I directly access my JSP?

分類Dev

Why can't I access my private variable inside a function in PHP?

分類Dev

Why can't I access this global variable?

分類Dev

How can I Select 1st, 2nd and 3rd values in different columns - Ms Access

分類Dev

Swift/XCode - Why can't I pass data to the next view controller when a tableViewCell is pressed?

分類Dev

Why can't I select cells in my WPF datagrid?

分類Dev

Why can't I use my column alias in WHERE clause?

分類Dev

Why can't I extend my desktop 12.04 in KDE?

分類Dev

Why can't I crash my system with a fork bomb?

分類Dev

Why can't I see remote video in my WebRTC app?

分類Dev

Why can't I open my txt file in Ubuntu?

分類Dev

Why can't I download my package through packagist?

分類Dev

How to get data from 3rd level table using joins [Laravel]

分類Dev

Why can I extend an arrow function if I can't access its output?

分類Dev

3rd party subscribtion integratgion - I'd like to give free premium subscribtion to my customers

分類Dev

Swift 3:CoreData FetchRequest

分類Dev

Why array.reduce returns NaN when I add 3rd object

分類Dev

Why can I get access to static method of my super class from child instance?

分類Dev

Why can't I iterate over a list of Firebase Img URLS, but can access each on individually?

分類Dev

Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

分類Dev

Can't access my service in a remote Kubernetes

分類Dev

Changed the user password, but now I can't access my encrypted folder

分類Dev

Why can't I access a protected method from a subclass in C#?

分類Dev

Why can't I access the model's property via the lambda expression parameter?

分類Dev

Why can't I copy a large number of files to my USB flash drive?

Related 関連記事

  1. 1

    Why can't I access my objects member variable?

  2. 2

    Why can't I access my JavaScript array by index outside of the d3 then function?

  3. 3

    Why can't I access data in parent with jquery?

  4. 4

    How can I login a client to a 3rd party site with basic access authentication from django

  5. 5

    Why can I directly access my JSP?

  6. 6

    Why can't I access my private variable inside a function in PHP?

  7. 7

    Why can't I access this global variable?

  8. 8

    How can I Select 1st, 2nd and 3rd values in different columns - Ms Access

  9. 9

    Swift/XCode - Why can't I pass data to the next view controller when a tableViewCell is pressed?

  10. 10

    Why can't I select cells in my WPF datagrid?

  11. 11

    Why can't I use my column alias in WHERE clause?

  12. 12

    Why can't I extend my desktop 12.04 in KDE?

  13. 13

    Why can't I crash my system with a fork bomb?

  14. 14

    Why can't I see remote video in my WebRTC app?

  15. 15

    Why can't I open my txt file in Ubuntu?

  16. 16

    Why can't I download my package through packagist?

  17. 17

    How to get data from 3rd level table using joins [Laravel]

  18. 18

    Why can I extend an arrow function if I can't access its output?

  19. 19

    3rd party subscribtion integratgion - I'd like to give free premium subscribtion to my customers

  20. 20

    Swift 3:CoreData FetchRequest

  21. 21

    Why array.reduce returns NaN when I add 3rd object

  22. 22

    Why can I get access to static method of my super class from child instance?

  23. 23

    Why can't I iterate over a list of Firebase Img URLS, but can access each on individually?

  24. 24

    Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

  25. 25

    Can't access my service in a remote Kubernetes

  26. 26

    Changed the user password, but now I can't access my encrypted folder

  27. 27

    Why can't I access a protected method from a subclass in C#?

  28. 28

    Why can't I access the model's property via the lambda expression parameter?

  29. 29

    Why can't I copy a large number of files to my USB flash drive?

ホットタグ

アーカイブ