If I have a method to attach an animation to an UI Element
, instead of copy/pasting the code for this method into many viewcontrollers
, can I just create one class
to hold the method and then import it?
如果我有一个将动画附加到UI元素的方法,而不是将该方法的代码复制/粘贴到许多viewcontrollers,我是否可以创建一个类来保存该方法,然后导入它?
I assume it doesn't make sense to use a category
in this instance because I need this method in multiple viewcontrollers
, and a category
is an extension of a single viewcontroller
.
我假设在这个实例中使用一个category是没有意义的,因为我需要在多个viewcontrollers中使用这个方法,而category是一个viewcontroller的扩展。
6 个解决方案
#1
16
A category is an extension of a class, not of a specific instance of a class. And, any modification that a category makes to a class is available to all subclasses (as with other methods on classes in OOP).
类别是类的扩展,而不是类的特定实例的扩展。而且,类别对类的任何修改都对所有子类可用(与OOP中类的其他方法一样)。
So, if you add a category on NSObject
, basically all classes in your entire app have access to the methods in that category - hence you need to be very careful what methods you add there.
所以,如果你在NSObject上添加一个类别,基本上你整个应用程序中的所有类都可以访问这个类别中的方法——因此你需要非常小心你在那里添加的方法。
Whether you add a category or a helper class is personal preference in a lot of cases, both will work.
在很多情况下,无论添加类别还是helper类都是个人偏好,两者都可以。
#2
12
Edited 9 April 2015
编辑2015年4月9日
You have two basic choices:
你有两个基本的选择:
Create a category, or create a custom subclass.
创建一个类别,或者创建一个自定义子类。
Categories
Categories allow you to add methods to an entire class, even classes for which you don't have the source code. Categories are especially useful for adding new methods to system classes where the system creates the object inside frameworks, where you can't change things to create a custom subclass instead.
类别允许您向整个类添加方法,甚至是没有源代码的类。类别对于向系统类添加新方法特别有用,在这些类中,系统在框架中创建对象,在框架中您不能更改内容来创建自定义子类。
You can add a category to a base class like UIView, and the methods you add become available to that class and all it's subclasses. So you could add an animation category to UIView and then all UIView objects including image views, buttons, sliders, etc, would gain the methods defined in that category.
你可以向UIView这样的基类添加一个类别,你添加的方法对该类和它所有的子类都是可用的。你可以向UIView添加一个动画类别然后所有UIView对象,包括图像视图,按钮,滑动器等等,都将会得到这个类别中定义的方法。
Categories help you get around the fact that there is no multiple inheritance in Objective-C. (In the example above of adding animation behavior to UIViews, you can't create a subclass of UIView AnimationView, and then create a UITextView that inherits from both UITextView and AnimationView and also create a UIImageView that inherits from AnimationView.)
类别可以帮助您避免Objective-C中没有多重继承。(在上面向UIView添加动画行为的示例中,您不能创建UIView AnimationView的子类,然后创建一个UITextView,它继承自UITextView和AnimationView,也不能创建继承自AnimationView的UIImageView)。
There are a couple of significant limitations to categories:
类别有几个重大的限制:
-
They can't really override the code of already-existing methods. (They can, but you can't call the super implementation, and if there are multiple categories with implementations of the same method, the results are undefined, so you should not do this.)
它们不能真正覆盖已经存在的方法的代码。(它们可以,但是不能调用super实现,如果有多个类别具有相同方法的实现,那么结果是未定义的,所以不应该这样做。)
-
Categories can't add new instance variables to the classes they extend. (There are ways to simulate this using a technique called associative storage, but that's beyond the scope of this post
类别不能向其扩展的类添加新的实例变量。(有一些方法可以使用称为关联存储的技术来模拟这种情况,但这超出了本文的范围。
Custom subclasses
A custom subclass can override existing methods. It can also add new methods, properties, and instance variables. However, it can't add methods to an existing subclass like a category can.
自定义子类可以覆盖现有方法。它还可以添加新的方法、属性和实例变量。但是,它不能像类别那样向现有的子类添加方法。
#3
2
Why use categories by example :
为什么按例子使用类别:
Lets say you have a base Class Engine that is inherited by Classes like Car, Bike and Scooter. Company was start-up and willing to add a functionality of maintenance that wasn't provided earlier.
假设您有一个基类引擎,它是由类继承的,比如Car、Bike和Scooter。公司刚刚起步,并愿意增加以前没有提供的维护功能。
Engine of Car, Bike and Scooter will be maintenaned under same policies. So it is better to add more functionalities on into Engine class instead of each Vehicle. In doing so we are not supposed to alter the Engine class for it may not be available to alter. Just create a category
汽车、自行车和踏板车的发动机将在同样的政策下维修。所以最好在引擎类中加入更多的功能,而不是每一辆车。在这样做时,我们不应该修改引擎类,因为它可能无法更改。创建一个类别
Engine+Maintenance
and all the functionalities you put in maintenance will be available in all the subclasses. A better approach.
您在维护中放置的所有功能在所有子类中都是可用的。一个更好的方法。
Categories are a way to split a single class definition into multiple files. Their goal is to ease the burden of maintaining large code bases by modularizing a class. This prevents your source code from becoming monolithic 10000+ line files that are impossible to navigate and makes it easy to assign specific, well-defined portions of a class to individual developers
类别是将一个类定义分割为多个文件的一种方法。他们的目标是通过模块化类来减轻维护大型代码库的负担。这可以防止您的源代码变成不可能导航的单片10000+行文件,并且可以很容易地将类的特定的、定义良好的部分分配给各个开发人员
#4
1
Categories are an alternate of sub-classing. When we add a method to an existing class, it becomes a part of that class methods. So, any instance of that class can access that method. However, it doesn't mean that when you add a method to some existing class like NSString or NSArray, the class is going to add your method to its definition universally. It only means that inside that project, your NSString or NSArray class will have another method defined by you, and any class(inside that particular project) that is sub-classing it, or creating an instance of that class will have that method as a part of that class' definition.
类别是子类别的替代。当我们向一个现有的类添加一个方法时,它就成为那个类方法的一部分。这个类的任何实例都可以访问这个方法。然而,这并不意味着当你向一些现有的类(如NSString或NSArray)添加一个方法时,这个类会将你的方法普遍地添加到它的定义中。它只意味着,在这个项目中,您的NSString或NSArray类将有另一个由您定义的方法,任何类(在那个特定的项目中)对它进行子类化,或创建该类的实例,都将该方法作为该类定义的一部分。
So, you can add your animation to your desired UI Element by adding a method through category to that UI Element's UIView class. Let's say your UI Element is a button. Now, if you create an category for the UIButton then, whenever you create an instance of UIButton, you can automatically have a method to animate it.
你可以通过category向UI元素的UIView类添加一个方法,将动画添加到你想要的UI元素中。假设你的UI元素是一个按钮。现在,如果你为UIButton创建一个类别,那么,每当你创建一个UIButton的实例,你就可以自动有一个方法来激活它。
#5
1
Subclassing is one way to add functionality to an object, but avoiding unnecessary subclassing by using a category will help reduce the amount of code and keep your projects more organized.
子类化是向对象添加功能的一种方式,但是通过使用类别避免不必要的子类化将有助于减少代码量并使项目更有条理。
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html
#6
0
categories are a subclass of specific class.for example if you want to change text color, background and font of label without programming code,then you have to make a catogories of UILabel class.
类别是特定类的子类。例如,如果您想要改变文本颜色、背景和没有编程代码的标签的字体,那么您就必须制作一个UILabel类的catog。
#1
16
A category is an extension of a class, not of a specific instance of a class. And, any modification that a category makes to a class is available to all subclasses (as with other methods on classes in OOP).
类别是类的扩展,而不是类的特定实例的扩展。而且,类别对类的任何修改都对所有子类可用(与OOP中类的其他方法一样)。
So, if you add a category on NSObject
, basically all classes in your entire app have access to the methods in that category - hence you need to be very careful what methods you add there.
所以,如果你在NSObject上添加一个类别,基本上你整个应用程序中的所有类都可以访问这个类别中的方法——因此你需要非常小心你在那里添加的方法。
Whether you add a category or a helper class is personal preference in a lot of cases, both will work.
在很多情况下,无论添加类别还是helper类都是个人偏好,两者都可以。
#2
12
Edited 9 April 2015
编辑2015年4月9日
You have two basic choices:
你有两个基本的选择:
Create a category, or create a custom subclass.
创建一个类别,或者创建一个自定义子类。
Categories
Categories allow you to add methods to an entire class, even classes for which you don't have the source code. Categories are especially useful for adding new methods to system classes where the system creates the object inside frameworks, where you can't change things to create a custom subclass instead.
类别允许您向整个类添加方法,甚至是没有源代码的类。类别对于向系统类添加新方法特别有用,在这些类中,系统在框架中创建对象,在框架中您不能更改内容来创建自定义子类。
You can add a category to a base class like UIView, and the methods you add become available to that class and all it's subclasses. So you could add an animation category to UIView and then all UIView objects including image views, buttons, sliders, etc, would gain the methods defined in that category.
你可以向UIView这样的基类添加一个类别,你添加的方法对该类和它所有的子类都是可用的。你可以向UIView添加一个动画类别然后所有UIView对象,包括图像视图,按钮,滑动器等等,都将会得到这个类别中定义的方法。
Categories help you get around the fact that there is no multiple inheritance in Objective-C. (In the example above of adding animation behavior to UIViews, you can't create a subclass of UIView AnimationView, and then create a UITextView that inherits from both UITextView and AnimationView and also create a UIImageView that inherits from AnimationView.)
类别可以帮助您避免Objective-C中没有多重继承。(在上面向UIView添加动画行为的示例中,您不能创建UIView AnimationView的子类,然后创建一个UITextView,它继承自UITextView和AnimationView,也不能创建继承自AnimationView的UIImageView)。
There are a couple of significant limitations to categories:
类别有几个重大的限制:
-
They can't really override the code of already-existing methods. (They can, but you can't call the super implementation, and if there are multiple categories with implementations of the same method, the results are undefined, so you should not do this.)
它们不能真正覆盖已经存在的方法的代码。(它们可以,但是不能调用super实现,如果有多个类别具有相同方法的实现,那么结果是未定义的,所以不应该这样做。)
-
Categories can't add new instance variables to the classes they extend. (There are ways to simulate this using a technique called associative storage, but that's beyond the scope of this post
类别不能向其扩展的类添加新的实例变量。(有一些方法可以使用称为关联存储的技术来模拟这种情况,但这超出了本文的范围。
Custom subclasses
A custom subclass can override existing methods. It can also add new methods, properties, and instance variables. However, it can't add methods to an existing subclass like a category can.
自定义子类可以覆盖现有方法。它还可以添加新的方法、属性和实例变量。但是,它不能像类别那样向现有的子类添加方法。
#3
2
Why use categories by example :
为什么按例子使用类别:
Lets say you have a base Class Engine that is inherited by Classes like Car, Bike and Scooter. Company was start-up and willing to add a functionality of maintenance that wasn't provided earlier.
假设您有一个基类引擎,它是由类继承的,比如Car、Bike和Scooter。公司刚刚起步,并愿意增加以前没有提供的维护功能。
Engine of Car, Bike and Scooter will be maintenaned under same policies. So it is better to add more functionalities on into Engine class instead of each Vehicle. In doing so we are not supposed to alter the Engine class for it may not be available to alter. Just create a category
汽车、自行车和踏板车的发动机将在同样的政策下维修。所以最好在引擎类中加入更多的功能,而不是每一辆车。在这样做时,我们不应该修改引擎类,因为它可能无法更改。创建一个类别
Engine+Maintenance
and all the functionalities you put in maintenance will be available in all the subclasses. A better approach.
您在维护中放置的所有功能在所有子类中都是可用的。一个更好的方法。
Categories are a way to split a single class definition into multiple files. Their goal is to ease the burden of maintaining large code bases by modularizing a class. This prevents your source code from becoming monolithic 10000+ line files that are impossible to navigate and makes it easy to assign specific, well-defined portions of a class to individual developers
类别是将一个类定义分割为多个文件的一种方法。他们的目标是通过模块化类来减轻维护大型代码库的负担。这可以防止您的源代码变成不可能导航的单片10000+行文件,并且可以很容易地将类的特定的、定义良好的部分分配给各个开发人员
#4
1
Categories are an alternate of sub-classing. When we add a method to an existing class, it becomes a part of that class methods. So, any instance of that class can access that method. However, it doesn't mean that when you add a method to some existing class like NSString or NSArray, the class is going to add your method to its definition universally. It only means that inside that project, your NSString or NSArray class will have another method defined by you, and any class(inside that particular project) that is sub-classing it, or creating an instance of that class will have that method as a part of that class' definition.
类别是子类别的替代。当我们向一个现有的类添加一个方法时,它就成为那个类方法的一部分。这个类的任何实例都可以访问这个方法。然而,这并不意味着当你向一些现有的类(如NSString或NSArray)添加一个方法时,这个类会将你的方法普遍地添加到它的定义中。它只意味着,在这个项目中,您的NSString或NSArray类将有另一个由您定义的方法,任何类(在那个特定的项目中)对它进行子类化,或创建该类的实例,都将该方法作为该类定义的一部分。
So, you can add your animation to your desired UI Element by adding a method through category to that UI Element's UIView class. Let's say your UI Element is a button. Now, if you create an category for the UIButton then, whenever you create an instance of UIButton, you can automatically have a method to animate it.
你可以通过category向UI元素的UIView类添加一个方法,将动画添加到你想要的UI元素中。假设你的UI元素是一个按钮。现在,如果你为UIButton创建一个类别,那么,每当你创建一个UIButton的实例,你就可以自动有一个方法来激活它。
#5
1
Subclassing is one way to add functionality to an object, but avoiding unnecessary subclassing by using a category will help reduce the amount of code and keep your projects more organized.
子类化是向对象添加功能的一种方式,但是通过使用类别避免不必要的子类化将有助于减少代码量并使项目更有条理。
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html
#6
0
categories are a subclass of specific class.for example if you want to change text color, background and font of label without programming code,then you have to make a catogories of UILabel class.
类别是特定类的子类。例如,如果您想要改变文本颜色、背景和没有编程代码的标签的字体,那么您就必须制作一个UILabel类的catog。