Swift has gotten me to finally give IOS development a try, but I'm having some issues with CoreData. I'm having no luck trying to access my properties that map to my one-to-many relationships.
Swift让我最终尝试了IOS开发,但是我遇到了一些与CoreData有关的问题。我没有运气试图访问我的属性,映射到我的一对多关系。
I've got a one-to-many relationship set up as such:
我已经建立了这样的一对多关系:
@objc(Project)
class Project: NSManagedObject {
@NSManaged var name:String
@NSManaged var scribbles : Array<Scribble>
}
@objc(Scribble)
class Scribble: NSManagedObject {
@NSManaged var notes:String
@NSManaged var project:Project
}
I fetch my data like this:
我像这样获取我的数据:
let request = NSFetchRequest(entityName: "Projects");
request.returnsObjectsAsFaults = false;
request.includesSubentities = true;
var results:NSArray = context.executeFetchRequest(request, error: nil);
for project in results
{
let thisProject:Project = project as Project;
projects.append(thisProject);
}
But whenever I try to iterate over the scribbles for a project using for..in I get this exception:
但每当我尝试使用for..in迭代一个项目的涂鸦时,我得到这个例外:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Scribble count]: unrecognized selector sent to instance
When I println on my project object It looks like I'm getting something back, I just don't know what to do with it.
当我在我的项目对象上打印它看起来我得到了回报,我只是不知道如何处理它。
<Project: 0xb48e010> (entity: Projects; id: 0xb48d340 <x-coredata://CDEFAC19-4D9E-426D-B0CC-EFFEE9053707/Projects/p3> ; data: {
name = Larry;
scribbles = "0xb48e7b0 <x-coredata://CDEFAC19-4D9E-426D-B0CC-EFFEE9053707/Scribbles/p3>";
})
1 个解决方案
#1
7
From the exception that's being thrown, it appears you're trying to iterate over a collection, but instead project.scribbles returning a collection, its returning an instance of Scribble.
从被抛出的异常开始,看起来你正试图迭代一个集合,而是project.scribbles返回一个集合,它返回一个Scribble实例。
That makes me think that your relationship is set up as 'to one' in the data modeler. Make sure that you've set the relationship to 'to many'. You can do that by selecting the relationship, and there will be an option in the Data Model Inspector called type - it's set to 'to one' by default.
这让我觉得你的关系在数据建模器中被设置为“一个”。确保您已将关系设置为“to many”。您可以通过选择关系来实现,并且数据模型检查器中将有一个名为type的选项 - 默认情况下它设置为“to one”。
#1
7
From the exception that's being thrown, it appears you're trying to iterate over a collection, but instead project.scribbles returning a collection, its returning an instance of Scribble.
从被抛出的异常开始,看起来你正试图迭代一个集合,而是project.scribbles返回一个集合,它返回一个Scribble实例。
That makes me think that your relationship is set up as 'to one' in the data modeler. Make sure that you've set the relationship to 'to many'. You can do that by selecting the relationship, and there will be an option in the Data Model Inspector called type - it's set to 'to one' by default.
这让我觉得你的关系在数据建模器中被设置为“一个”。确保您已将关系设置为“to many”。您可以通过选择关系来实现,并且数据模型检查器中将有一个名为type的选项 - 默认情况下它设置为“to one”。