错误:“'NSManagedObjectContext' 不能转换为 'UInt8'”
Posted
技术标签:
【中文标题】错误:“\'NSManagedObjectContext\' 不能转换为 \'UInt8\'”【英文标题】:Error: "'NSManagedObjectContext' is not convertible to 'UInt8'"错误:“'NSManagedObjectContext' 不能转换为 'UInt8'” 【发布时间】:2015-04-02 06:59:03 【问题描述】:This is the core data tutorial 我正在尝试完成。错误在saveContext()
函数中。
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
【问题讨论】:
请将标题中的错误添加到帖子中 顺便说一句,if !(_managedObjectContext != nil)
("If _managedObjectContext
is not not 等于 nil
") 是一种奇怪的方式写if _managedObjectContext == nil
(“如果_managedObjectContext
等于nil
”)。
【参考方案1】:
在saveContext()
中,您的常量managedObjectContext
的类型为NSManagedObjectContext
,但在下一行中,您将针对nil
对其进行测试,这是不可能的。只有可选类型(例如NSManagedObjectContext?
)可以是nil
。有关详细信息,请参阅 The Swift Programming Language: The Basics 中的“可选”。
这可能是您的错误的原因,尽管从代码和错误中不清楚它所指的是什么。 Swift 的编译器错误在语言开发的早期阶段并不是非常有用,因此有时需要小心处理。
【讨论】:
非常感谢您分享和提供教育资源。我将从您共享的链接中查看选项。另外,感谢您更新我的问题。【参考方案2】:我不能肯定地给你答案,但是你可以试试这个 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:: - 你的标签在这里
【讨论】:
这不会编译。可选绑定需要if let
表达式计算为可选值 - self.managedObjectContext
在这里不是可选的。
你是对的,我将添加我的 managedObject 以便更有意义以上是关于错误:“'NSManagedObjectContext' 不能转换为 'UInt8'”的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Interface Builder 的 XIB 传递 NSManagedObjectContext
MagicalRecord MR_saveToPersistentStoreWithCompletion 极慢
无法将表达式的类型“NSDictionary”转换为类型“StringLiteralConvertible”
iOS - 核心数据 - NSManagedObjectContext - 不确定是不是保存
远程服务器返回错误: 404错误远程服务器返回错误:500错误 HttpWebResponse远程服务器返回错误:(404500) 错误。