我应该选择哪一个,子类或类别

时间:2022-02-12 19:18:00

I have UIlabel and includes few customisation of it . and this label is used in so many screens in my application. i tried with both subclassing and categories in prototype. which is preferred one to use in this case. subclassing or categories.

我有UIlabel并且包含很少的自定义。这个标签在我的应用程序中用于很多屏幕。我尝试了原型中的子类化和类别。在这种情况下,这是首选使用的。子类或类别。

2 个解决方案

#1


2  

Scenario - 1 : If you want to change something which happens as part of framework calls during the lifecycle of a UI object? - use subclass.

场景 - 1:如果要在UI对象的生命周期中更改作为框架调用的一部分而发生的事情? - 使用子类。

Scenario - 2 : If you want something application wide, something which is in addition to the existing functionality, and you don't care if this becomes available to all instances of this pre-existing instances of the framework class?
 - use categories.

场景-2:如果你想要一些应用程序范围内的东西,除现有功能之外还有什么东西,你不关心它是否可用于这个预先存在的框架类实例的所有实例? - 使用类别。

#2


0  

You should use Subclassing instead of category. Category should be used when you want to alter the behavior of every instance of class while Subclassing should be used when you want to alter behavior of specific instance or objects.

您应该使用Subclassing而不是category。当您想要更改每个类实例的行为时,应使用类别,而当您想要更改特定实例或对象的行为时,应使用子类。

Category is harmful also in some cases as you are manipulation existing class so it is better to use Subclassing where possible!!

在某些情况下类别也是有害的,因为你操纵现有的类,所以最好尽可能使用Subclassing !!

#1


2  

Scenario - 1 : If you want to change something which happens as part of framework calls during the lifecycle of a UI object? - use subclass.

场景 - 1:如果要在UI对象的生命周期中更改作为框架调用的一部分而发生的事情? - 使用子类。

Scenario - 2 : If you want something application wide, something which is in addition to the existing functionality, and you don't care if this becomes available to all instances of this pre-existing instances of the framework class?
 - use categories.

场景-2:如果你想要一些应用程序范围内的东西,除现有功能之外还有什么东西,你不关心它是否可用于这个预先存在的框架类实例的所有实例? - 使用类别。

#2


0  

You should use Subclassing instead of category. Category should be used when you want to alter the behavior of every instance of class while Subclassing should be used when you want to alter behavior of specific instance or objects.

您应该使用Subclassing而不是category。当您想要更改每个类实例的行为时,应使用类别,而当您想要更改特定实例或对象的行为时,应使用子类。

Category is harmful also in some cases as you are manipulation existing class so it is better to use Subclassing where possible!!

在某些情况下类别也是有害的,因为你操纵现有的类,所以最好尽可能使用Subclassing !!