这就是正在发生的事情:我是 swift 新手,我还不太了解 CoreData,但我正在关注 youtube 上的一些教程,以便学习它。现在,当我运行我的应用程序时,它会崩溃,然后出现错误“致命错误:在展开可选值时意外发现 nil”
详细信息:此代码来自一个帮助新手快速使用 CoreData 的教程。
这是代码:
导入 UIKit 导入 CoreData
类 SwiftCoreDataHelper: NSObject {
class func directoryForDatabaseFilename()->NSString{
return NSHomeDirectory().stringByAppendingString("/Library/Private Documents")
}
class func databaseFilename()->NSString{
return "database.sqlite";
}
class func managedObjectContext()->NSManagedObjectContext{
var error:NSError? = nil
NSFileManager.defaultManager().createDirectoryAtPath(SwiftCoreDataHelper.directoryForDatabaseFilename() as String, withIntermediateDirectories: true, attributes: nil, error: &error)
let path:NSString = "\(SwiftCoreDataHelper.directoryForDatabaseFilename()) + \(SwiftCoreDataHelper.databaseFilename())"
let url:NSURL = NSURL(fileURLWithPath: path as String)!
let managedModel:NSManagedObjectModel = NSManagedObjectModel.mergedModelFromBundles(nil)!
var storeCoordinator:NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedModel)
if !(storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error:&error ) != nil){
if (error != nil){
println(error!.localizedDescription)
abort()
}
}
var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = storeCoordinator
return managedObjectContext
}
class func insertManagedObject(className:NSString, managedObjectConect:NSManagedObjectContext)->AnyObject{
let managedObject:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName(className as String, inManagedObjectContext: managedObjectConect) as! NSManagedObject
return managedObject
}
class func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool{
if managedObjectContext.save(nil){
return true
}else{
return false
}
}
class func fetchEntities(className:NSString, withPredicate predicate:NSPredicate?, managedObjectContext:NSManagedObjectContext)->NSArray{
let fetchRequest:NSFetchRequest = NSFetchRequest()
let entetyDescription:NSEntityDescription = NSEntityDescription.entityForName(className as String, inManagedObjectContext: managedObjectContext)!
fetchRequest.entity = entetyDescription
if (predicate != nil){
fetchRequest.predicate = predicate!
}
fetchRequest.returnsObjectsAsFaults = false
let items:NSArray = managedObjectContext .executeFetchRequest(fetchRequest, error: nil)!
return items
}
}
然后,它就在这里崩溃:
**let entetyDescription:NSEntityDescription = NSEntityDescription.entityForName(className as String, inManagedObjectContext: managedObjectContext)!** // Crashes right here, "Thead 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0)
我应该怎么做才能修复这个错误?我在这个线程中待了 3 天,我不知道在哪里运行!
提前致谢。
更新
这是我的代码:
导入 UIKit 导入 CoreData
类 MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var mainImage: UIImageView!
@IBOutlet weak var tableView: UITableView!
var namesListArray:NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
loadData()
}
func loadData() {
namesListArray.removeAllObjects()
let moc:NSManagedObjectContext = SwiftCoreDataHelper.managedObjectContext()
let results:NSArray = SwiftCoreDataHelper.fetchEntities(NSStringFromClass(Recipe), withPredicate: nil, managedObjectContext: moc)
for recipe in results {
let singleRecipe:Recipe = recipe as! Recipe
let recipeDict:NSDictionary = ["identifier":singleRecipe.identifier, "recipelabel":singleRecipe.recipelabel, "recipeimage":singleRecipe.recipeimage]
namesListArray.addObject(recipeDict)
}
tableView.reloadData() }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesListArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomCellTableViewCell
let recipeDict:NSDictionary = namesListArray.objectAtIndex(indexPath.row) as! NSDictionary
let recipeName = recipeDict.objectForKey("recipelabel") as! String
let imageData:NSData = recipeDict.objectForKey("recipeimage") as! NSData
let recipeImage: UIImage = UIImage(data: imageData)!
cell.customLabel.text = recipeName
var recipeImageFrame:CGRect = cell.customImage.frame
recipeImageFrame.size = CGSizeMake(75,75)
cell.customImage.frame = recipeImageFrame
cell.customImage.image = recipeImage
return cell
}
}
谓词 NSPredicate? 无 无
也许错误就在这里?如果是这样,我看不到它。任何想法?
再次感谢!!!