ios - 无法为实体 'MyLocations.Location' 加载名为 'Location' 的类

标签 ios iphone swift core-data

当我创建核心数据文件并将此代码添加到完成方法中以保存数据后,我收到了上述主题的错误,即无法找到类。

它还显示错误

Class not found, using default NSManagedObject instead

@IBAction func done()
{
    let hudView = HudView.hudInView(navigationController!.view, animated: true)

    hudView.text = "Tagged"

    let location = NSEntityDescription.insertNewObjectForEntityForName("Location", inManagedObjectContext: managedObjectContext) as! Location

    location.locationDescription = descriptionText

    location.category = categoryName

    location.latitude = coordinate.latitude

    location.longitude = coordinate.longitude

    location.date = date

    location.placemark = placemark

    var error: NSError?

    if !managedObjectContext.save(&error)
    {
        println("Error: \(error)")

        abort()
    }

    afterDelay(0.6) {self.dismissViewControllerAnimated(true, completion: nil)}
}

最佳答案

您必须在 xcdatamodeld 文件中设置该类。 转到您的项目名称.xcdatamodeld -> 选择位置表,然后选择数据模型检查器(如下面的屏幕截图所示)并设置类。

enter image description here

希望这会有所帮助。

在代码中进行以下更改

 var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
 var context: NSManagedObjectContext = appDel11.managedObjectContext!
 var location = NSEntityDescription.insertNewObjectForEntityForName("Location", inManagedObjectContext: context) as! NSManagedObject

关于ios - 无法为实体 'MyLocations.Location' 加载名为 'Location' 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043469/

相关文章:

ios - 为 iOS 推送通知导入 SSL 证书会阻止其他开发人员处理该项目吗?

iphone - iOS 7 Safari : OS locks up for 4 seconds when clicking/focusing on a HTML input

ios - 从 iPhone 设备上传视频失败但在模拟器上完美运行错误 :'Cannot read file'

ios - 使用performSegueWithIdentifier时的Segue转换

swift SKSpriteNode : Complex Sprite Textures?

ios - Xamarin 表单 - IOS : How to detect the UIView size changed

ios - 使用 SWRevealViewController 时获取顶部 UIViewController

ios - 将 Metal 深度缓冲区与场景套件渲染集成

ios - 为什么 Main.storyboard 不显示?

ios - Xcode 6 Beta 5 中的 UIView.animateWithDuration 更改