We have some code in objective-c categories which we want to share between projects. There are (at least) two approaches we can take:
我们在objective-c类别中有一些代码,我们希望在项目之间共享这些代码。我们至少可以采取两种方法:
- Put them in one category per class, called something like
UIView+SGBExtensions
- 把它们放到一个类中,叫做UIView+ sgbextense
- Put then in a number of different categories by use, e.g.
UIView+SGBLayout
,UIView+SGBDrawing
, etc.. - 然后使用一些不同的类别,例如UIView+SGBLayout, UIView+SGBDrawing等等。
My instinct is to go with the latter, as it will be more descriptive and we can cherry-pick. However, most of our apps will include most of the shared code, so I'm a little concerned that having a lot of categories might impact performance or app size. Is there a drawback to having many objective-c categories?
我的直觉是选择后者,因为它将更具描述性,我们可以挑选。然而,我们的大多数应用程序将包含大部分共享代码,所以我有点担心,大量的类别可能会影响性能或应用程序大小。有许多objective-c类别有缺点吗?
2 个解决方案
#1
3
I think the difference would be negligible. During loading, it may involve more steps to iterate over the categories and add their methods, rather than adding methods from a single category. Likewise, if the categories have +load
methods, that's several method calls rather than a single one. Like I said, negligible.
我认为差别可以忽略不计。在加载过程中,可能需要更多的步骤来遍历类别并添加它们的方法,而不是从单个类别中添加方法。同样,如果类别有+load方法,那就是多个方法调用,而不是单个方法调用。就像我说的,可以忽略不计。
#2
2
My instinct is the same as yours. Segregating the code out your way is more maintainable.
我的直觉和你的一样。以自己的方式隔离代码更容易维护。
I don't think you should worry about size and performance until your application has been demonstrated to have a problem in those respects. Even then, don't assume, measure. For what it's worth, I'm pretty sure that the performance and size impact will be close to zero.
我认为,在应用程序被证明在这些方面存在问题之前,不应该担心大小和性能。即便如此,也不要想当然地去衡量。对于它的价值,我很确定性能和大小的影响将接近于零。
#1
3
I think the difference would be negligible. During loading, it may involve more steps to iterate over the categories and add their methods, rather than adding methods from a single category. Likewise, if the categories have +load
methods, that's several method calls rather than a single one. Like I said, negligible.
我认为差别可以忽略不计。在加载过程中,可能需要更多的步骤来遍历类别并添加它们的方法,而不是从单个类别中添加方法。同样,如果类别有+load方法,那就是多个方法调用,而不是单个方法调用。就像我说的,可以忽略不计。
#2
2
My instinct is the same as yours. Segregating the code out your way is more maintainable.
我的直觉和你的一样。以自己的方式隔离代码更容易维护。
I don't think you should worry about size and performance until your application has been demonstrated to have a problem in those respects. Even then, don't assume, measure. For what it's worth, I'm pretty sure that the performance and size impact will be close to zero.
我认为,在应用程序被证明在这些方面存在问题之前,不应该担心大小和性能。即便如此,也不要想当然地去衡量。对于它的价值,我很确定性能和大小的影响将接近于零。