2016-08-29 22 views
10

Chcę pracować poprzez this example code, w której Swift i CoreData są używane do utworzenia tabeli. Jednak za pomocą Swift 3 nie uda mi się go uruchomić. Co najważniejsze, nie mogę prawidłowo zamienić linięmanagedObjectContext in Swift 3

// set up the NSManagedObjectContext 
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate 
    managedContext = appDelegate.managedObjectContext 

chociaż znalazłem this related question (która jednak nie jest iOS OS X). Jak mogę zastąpić ten fragment kodu, który generuje komunikat o błędzie Value of type 'AppDelegate' has no member 'managedContext'?

+0

Czy podczas tworzenia nowego projektu zaznaczyłeś opcję "Użyj danych podstawowych"? Jest to wymagane, ponieważ dodaje kod dla Core Data Stack w AppDelegate. – vadian

+0

@vadian Tak, zrobiłem. Ale: Sprawdziłem także aplikacje oparte na dokumentach, testy jednostkowe i testy interfejsu użytkownika. I zauważam, że żaden kod nie jest w AppDelegate, kiedy sprawdzam wszystko, w przeciwieństwie do sprawdzania tylko CoreData ... – DaPhil

+0

To dziwne. Złóż błąd. Aby rozwiązać problem, utwórz nowy projekt z zaznaczonymi tylko danymi podstawowymi i skopiuj i wklej stos danych podstawowych do projektu opartego na dokumencie. – vadian

Odpowiedz

13

Swift 3 w MacOS

let appDelegate = NSApplication.shared().delegate as! AppDelegate 
let managedContext = appDelegate.managedObjectContext 

Błąd podałeś mówi 'AppDelegate' has no member 'managedContext' zamiast 'AppDelegate' has no member 'managedObjectContext', który doprowadzi mnie do przyjęcia wystarczy naprawić składni.

Swift 3 w iOS 10

Rdzeń danych potrzebuje co najmniej 3 rzeczy do pracy:

  1. zarządzany obiekt modelu
  2. Trwałe sklep koordynator
  3. I udało kontekst obiektu

Połóż te trzy rzeczy s razem i otrzymasz stos danych rdzenia.

Po wyjściu iOS 10 został wprowadzony nowy obiekt o nazwie NSPersistentContainer, który hermetyzuje główny stos danych.

Jak utworzyć obiekt kontenera, odpowiada here.

managedObjectContext jest teraz właściwość o nazwie viewContext, dostępne poprzez:

let delegate = UIApplication.shared.delegate as! AppDelegate 
let managedObjectContext = delegate.persistentContainer.viewContext 

Pomocne artykuł jest What's New in Core Data, ale jeśli to czytanie wydaje się nieco zbyt ciężkie, to WWDC video robi wielką robotę wyjaśniając ten wątek.

2

I Swift 3 można uzyskać managedContext ustawiony przez ten kod:

let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
7

AppDelegate ma poniżej członków tylko

// MARK: - Core Data stack 

lazy var persistentContainer: NSPersistentContainer = { 
    /* 
    The persistent container for the application. This implementation 
    creates and returns a container, having loaded the store for the 
    application to it. This property is optional since there are legitimate 
    error conditions that could cause the creation of the store to fail. 
    */ 
    let container = NSPersistentContainer(name: "") 
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in 
     if let error = error as NSError? { 
      // Replace this implementation with code to handle the error appropriately. 
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

      /* 
      Typical reasons for an error here include: 
      * The parent directory does not exist, cannot be created, or disallows writing. 
      * The persistent store is not accessible, due to permissions or data protection when the device is locked. 
      * The device is out of space. 
      * The store could not be migrated to the current model version. 
      Check the error message to determine what the actual problem was. 
      */ 
      fatalError("Unresolved error \(error), \(error.userInfo)") 
     } 
    }) 
    return container 
}() 

więc używać

let managedContext = (UIApplication.shared.delegate as! appDelegate).persistentContainer.viewContext 

to będzie działać dobrze

2

Dla MacOS i Swift 3.1

let moc: NSManagedObjectContext = (NSApplication.shared().delegate as! AppDelegate).persistentContainer.viewContext