NSManagedObjects with Categories

北慕城南 提交于 2020-01-02 09:59:12

问题


I believe it is a common practice to put custom code for a NSManagedObject in a category of the subclass since Xcode will overrite your generated subclass when the model is edited. I just wanted to confirm this. I have seen examples where people say it is bad to combine categories with methods that are already implemented in the class hierarchy. I'm not sure if this is just for the cases when the class that has the actual category has the method already implemented or in all cases.


回答1:


The problem with overriding a method in a category is that you can't call the original implementation like you'd normally do using [super doSomething]. Usually, when you override a method, you still want to be able to call the original implementation doing something extra before and/or after the original implementation gets executed.

One fairly clean solution is to let Xcode generate NSManagedObject subclasses that you don't touch directly. Instead, create another, custom subclass of each Xcode-generated NSManagedObject subclass that you can edit without worrying about it being overwritten.

Wolf Rentzsch's mogenerator essentially uses this approach, and additionally generates some nice convenience methods that Xcode doesn't generate. Might be worth checking out because it can be a helpful tool, but mogenerator is not necessary to do what you're trying to do.



来源:https://stackoverflow.com/questions/9297101/nsmanagedobjects-with-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!