本文介绍了扩展Core Data生成的NSManagedObject类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我第一次处理Core Data,我想知道扩展Xcode为NSManagedObject实体生成的类的最佳做法。I've been dealing with Core Data for the first time and I wanted to know what the best practices are for extending the classes that Xcode generates for my NSManagedObject entities.我看到 mogenerator ,我也使用类似的方法, a href =http://www.cimgf.com/2011/03/13/super-happy-easy-fetching-in-core-data/ =nofollow>在内核数据中超级快速容易化。 所以我有三种类:I saw mogenerator and I've been also using a similar approach to that as suggested in SUPER HAPPY EASY FETCHING IN CORE DATA.So I had three kinds of classes: EasyFetching类别(只有一个类); 生成的NSManagedObject子类(即:_Entity); 一些自定义子类,包含查找所有非活动对象,清除对象的缓存等自定义方法:Entity)。 这种方法让我做一些自定义代码,而我可以重构我的Core Data实体并生成它多次我需要。但是我也遇到了一些问题,如不能为我的实体声明对象级方法(因为NSManagedObjectContext只知道我的_Entity类)。This approach let me do some custom code while I could refactor my Core Data entity and generate it as many times as I needed. But I've also run into some problems like not being able to declare object level methods for my entities (because the NSManagedObjectContext only knew my _Entity classes). m使用类别来扩展我的实体功能。这工作得更好,因为我可以有自定义对象级别的方法。我现在有三种类:Now I'm using categories to extend my entities functionalities. And this works a lot better because I can have custom object level methods. I now have three kinds of classes: EasyFetching类(因为它有很多方法,所有我的自定义代码使用) / li> 生成的NSManagedObject子类(即:Entity); 我的NSManagedObject实体的自定义类别(即:Entity + Custom.h) li> The EasyFetching category (as it has a lot of methods that all my custom code uses);A generated NSManagedObject subclass (i.e.: Entity);A custom category for my NSManagedObject entity (i.e.: Entity+Custom.h).我的问题是:您会推荐什么?My question is: what would you recommend?答案推荐答案现在您已将问题作为答案发布在我的问题,我想我应该回答你的问题:)Now that you have posted your question as an answer on my question,I thought I should answer your question :) Mogenerator看起来不好看,试试看。Mogenerator doesn't look bad, give it a try.你建议的类别也是一个很好的选择。 Also the way you suggested with categories is also a fine option. Infact这里是 link ,正确解释了如何这样做。Infact here is a link that exactly explains How to do so. 这篇关于扩展Core Data生成的NSManagedObject类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 03:37