【问题标题】:Unable to load class named 'MyLocations.Location' for entity 'Location'无法为实体“位置”加载名为“MyLocations.Location”的类
【发布时间】:2015-08-17 05:42:52
【问题描述】:

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

它也显示错误

找不到类,使用默认的 NSManagedObject 代替

@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)}
}

【问题讨论】:

  • 你的项目目录中有 Location 类吗?
  • 是的,但位置文件不是位置类
  • 我的项目目录中的项目中也有位置类
  • 是“类位置:NSManagedObject {”这样的吗?
  • 是的,和你说的一样。

标签: ios iphone swift core-data


【解决方案1】:

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

希望这可能会有所帮助。

在您的代码中进行以下更改

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

【讨论】:

  • 得到同样的错误。但我之前已经这样做了,但它不起作用。
  • 能不能把Location文件拖到项目主目录下再试试?
猜你喜欢
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 2016-04-05
  • 1970-01-01
  • 2014-12-24
相关资源
最近更新 更多