NSManagedObjects与类别 [英] NSManagedObjects with Categories

查看:137
本文介绍了NSManagedObjects与类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这是一个常见的做法,将一个 NSManagedObject 的自定义代码放入子类别中,因为Xcode会在编辑模型时覆写您生成的子类。我只是想证实这一点。我看到的例子,人们说,结合类别和已经实现在类层次结构中的方法是不好的。我不知道这是否只是为具有实际类别的类的方法已经实现或在所有情况下的情况。

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.

推荐答案

重写类别中的方法的问题是,您不能像通常那样调用原始实现使用 [super doSomething] 。通常,当你覆盖一个方法时,你仍然希望能够调用最初的实现,在原来的实现被执行之前和/或之后做一些额外的事情。

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.

干净的解决方案是让Xcode生成NSManagedObject子类,你不直接接触。相反,创建每个Xcode生成的NSManagedObject子类的另一个自定义子类,您可以编辑它,而不必担心它被覆盖。

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的 mogenerator 本质上使用这种方法,并且另外生成Xcode不生成的一些不错的方便方法。可能值得一试,因为它可以是一个有用的工具,但是mogenerator不必做你想做的事情。

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.

这篇关于NSManagedObjects与类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆