每种方法一个类别与一个类别中的所有方法? (对象C)

发布于 2024-09-08 10:54:37 字数 112 浏览 2 评论 0原文

我应该将所有方法放在一个类别中(我的意思是针对同一类型的对象)还是应该将它们分成多个文件? (类似于 NSStringAdditions 与 NSString+this 和 NSString+that 的比较)

Should I put all my methods in one category (I mean for the same kind of object) or should I split them up in many files? (Something like NSStringAdditions vs. NSString+this and NSString+that)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-09-15 10:54:37

这很大程度上取决于。

如果它们彼此密切相关,则将它们归为同一类别。如果它们解决的任务完全不同,那么将它们分开是合适的。

另外,在不同的项目中重用也是一个需要考虑的问题。

不过,请务必谨慎对待类别。尽管可以扩展现有的类,并且在某些情况下使用是合理的,但不要滥用它们。它们是有代价的(性能、灵活性、维护)。因此,如果您必须决定拆分您的类别,这在大多数情况下都表明这里过于复杂,而子类或其他辅助类是更好的选择。

That very much depends.

If they are closely related to each other, put them in the same category. If they solve quite different tasks, then splitting them is appropriate.

Also, reusing in different projects my be a point to consider.

Always treat categories with care, though. Although it is possible to extend existing classes, and in some cases it is justified use, don't abuse them. They come at a cost (performance, flexibility, maintenance). So if you have to decide to split your category, this is most of the time a sign for too much complexity here, and a subclass or other helper class is the better choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文