2015-04-02 45 views
-1

This is the core data tutorial我試圖完成。該錯誤在saveContext()函數中。錯誤:「'NSManagedObjectContext'不能轉換爲'UInt8'」

import UIKit 
import CoreData 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    // ... 

    func saveContext() { 
     var error: NSError? = nil 
     // There second line below this comment is providing for the error referenced in the question title. 
     let managedObjectContext = self.managedObjectContext 
     if (managedObjectContext != nil) 
     { 
      if managedObjectContext.hasChanges && !managedObjectContext.save(&error) { 
       // Replace this implementation with code to handle the error appropriately. 
       // abort() 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. 
       //println("Unresolved error \(error), \(error.userInfo)") 
       abort() 
      } 
     } 
    } 

    // #pragma mark - Core Data stack 

    // Returns the managed object context for the application. 
    // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
    var managedObjectContext: NSManagedObjectContext { 
     if !(_managedObjectContext != nil) { 
      let coordinator = self.persistentStoreCoordinator 
      if (coordinator != nil) { 
       _managedObjectContext = NSManagedObjectContext() 
       _managedObjectContext!.persistentStoreCoordinator = coordinator 
      } 
     } 
     return _managedObjectContext! 
    } 
    var _managedObjectContext: NSManagedObjectContext? = nil 

    // Returns the managed object model for the application. 
    // If the model doesn't already exist, it is created from the application's model. 
    var managedObjectModel: NSManagedObjectModel { 
     if (_managedObjectModel != nil) { 
      let modelURL = NSBundle.mainBundle().URLForResource("ContactU", withExtension: "momd") 
      _managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL!) 
     } 
     return _managedObjectModel! 
    } 
    var _managedObjectModel: NSManagedObjectModel? = nil 

    // Returns the persistent store coordinator for the application. 
    // If the coordinator doesn't already exist, it is created and the application's store added to it. 
    var persistentStoreCoordinator: NSPersistentStoreCoordinator { 
     if !(_persistentStoreCoordinator != nil) { 
      let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("ContactU.sqlite") 
      var error: NSError? = nil 
      _persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 
      if _persistentStoreCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) == nil { 
       /* 
       Replace this implementation with code to handle the error appropriately. 

       abort() 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 persistent store is not accessible; 
       * The schema for the persistent store is incompatible with current managed object model. 
       Check the error message to determine what the actual problem was. 


       If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

       If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
       * Simply deleting the existing store: 
       NSFileManager.defaultManager().removeItemAtURL(storeURL, error: nil) 

       * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
       [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] 

       Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

       */ 
       //println("Unresolved error \(error), \(error!.userInfo)") 
       abort() 
      } 
     } 
     return _persistentStoreCoordinator! 
    } 
    var _persistentStoreCoordinator: NSPersistentStoreCoordinator? = nil 

    // #pragma mark - Application's Documents directory 

    // Returns the URL to the application's Documents directory. 
    var applicationDocumentsDirectory: NSURL { 
     let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
     return urls[urls.count-1] as NSURL 
    } 
} 
+0

請將標題中的錯誤添加到文章 – Alexander 2015-04-02 07:10:57

+0

另外,if!(_ managedObjectContext!= nil)'(「If _managedObjectContext'不是_not_等於'nil如果_managedObjectContext == nil'(「if _managedObjectContext'等於'nil'」),那麼'')是一種奇怪的寫法。 – Stuart 2015-04-02 07:35:55

回答

0

saveContext()你不斷managedObjectContextNSManagedObjectContext型的,但在下一行正在測試其對nil,這是永遠不可能的。只有可選類型(例如NSManagedObjectContext?)可以是nil。有關更多信息,請參閱The Swift Programming Language: The Basics中的「可選項」。

這可能是你錯誤的原因,儘管從代碼和錯誤中不清楚它被引用的是什麼。在語言開發的早期階段,Swift的編譯器錯誤並不是非常有用,所以有時需要用一點鹽來進行。

+0

非常感謝您分享和提供教育資源。我會從你分享的鏈接看看選項。另外,感謝您更新我的問題。 – 2015-04-02 17:08:14

-1

我不能給你肯定的答案,但是你可以試試這個saveContext()方法

lazy var managedObjectContext: NSManagedObjectContext? = 
{ 
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. 
    let coordinator = self.persistentStoreCoordinator 

    if coordinator == nil 
    { 
     return nil 
    } 
    var managedObjectContext = NSManagedObjectContext() 
    managedObjectContext.persistentStoreCoordinator = coordinator 

    return managedObjectContext 
}() 

func saveContext() 
{ 
    if let moc = self.managedObjectContext 
    { 
     var error: NSError? = nil 
     if moc.hasChanges && !moc.save(&error) { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() 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. 
      NSLog("Unresolved error \(error), \(error!.userInfo)") 
      abort() 
     } 
    } 
} 

另外,如果你想純SWIFT你不應該使用「的#pragma」,只需使用// MARK :: - 您的標籤在這裏

+0

這不會編譯。可選綁定要求'if let'表達式計算爲可選值 - 'self.managedObjectContext'在此處不是可選的。 – Stuart 2015-04-02 17:45:24

+0

你是對的,我會添加我的managedObject,使它更有意義 – 2015-04-07 08:09:43

相關問題