Objective-c -类别和协议的目的

时间:2022-09-07 10:50:44

I've been reading up on Objective-c Protocols and Categories and both seem rather pointless to me. They're both used for adding things to the program in some funny way instead of just manually adding it.

我一直在研读Objective-c协议和类别,对我来说它们都是毫无意义的。它们都用于以某种有趣的方式向程序添加内容,而不是手工添加。

I might be convinced to see the purpose of the protocol being so that you can check the type of id's, but that's really about it. I see no other real use for them.

我可能会被说服去看协议的目的,这样你就可以检查id的类型了,但这是真的。我看不出它们还有什么真正的用处。

As for categories, I really don't see why you wouldn't just add them to the code. Why do you need to manually specify methods in a category that you're then gonna implement as opposed to just doing it normally? Is it that you might wanna make a "subclass" but with slight modifications? Why not just make a subclass?

至于类别,我真的不明白为什么不直接将它们添加到代码中。为什么你需要手动地指定一个类别中的方法,而不是像平常那样执行呢?你可能想要创建一个“子类”,但是需要稍微修改一下吗?为什么不创建一个子类呢?

I really don't see the purpose of either of these, I hope someone can tell me their real use =/

我真的不明白其中任何一个的目的,我希望有人能告诉我它们的真正用途=/

Thanks in advance, Christian

提前谢谢,基督徒

5 个解决方案

#1


15  

Categories are to add methods to classes whose source is unavailable to you, such as all the Apple classes (those starting with NS, CG, CA, etc.), without needing to subclass them.

类别是向那些源代码不可用的类添加方法,例如所有Apple类(以NS、CG、CA等开头的类),而不需要对它们进行子类化。

The purpose of protocols is to define methods that classes adhering to that protocol have to implement. In Java, those are called interfaces. The purpose is to codify similarities between classes that are not siblings (subclasses of the same superclass). Suppose you have a class Chair and a class Petrol. These don't have a lot in common, except that they both adhere to the flammable protocol, which requires them to have certain methods, such as specificEnergy and flamingPoint.

协议的目的是定义遵循该协议的类必须实现的方法。在Java中,这些被称为接口。目的是将非兄弟类(同一个超类的子类)之间的相似之处归类。假设你有一把椅子和一把汽油。它们之间没有很多共同点,只是它们都遵守可燃协议,这就要求它们有特定的方法,比如特定的能量和火焰点。

Now your Fire class can have a method addFlammableMaterial:(id <flammable>)material.

现在您的消防类可以有一个方法添加易燃材料:(id <易燃> )材料。

Protocols are often used to declare that instances of certain classes can be delegates for certain other instances. You can declare your view controller to act as the data source to a UITableView by declaring it to conform to the UITableViewDataSource protocol, which means your viewController guarantees that it implements the required methods of that protocol, and the tableView can rest safely because it can trust the VC to be its data source.

协议通常用于声明某些类的实例可以为某些其他实例的委托。你可以声明视图控制器作为数据源的UITableView声明符合需要的协议,这意味着你viewController保证它实现了协议的要求的方法,和表视图可以休息安全,因为它可以信任VC数据源。

#2


4  

A category is a way of adding new methods to all instances of an existing class without modifying the class itself.

类别是一种向现有类的所有实例添加新方法的方法,而无需修改类本身。

For category read the already discussed SO post,

对于类别阅读已经讨论过的文章,

What is the difference between inheritance and Categories in Objective-C

在Objective-C中继承和类别的区别是什么

protocol-versus-category

protocol-versus-category

Protocols are used frequently to specify the interface for delegate objects.

协议经常用于指定委托对象的接口。

Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:

协议声明可以被任何类实现的方法。协议在至少三种情况下是有用的:

To declare methods that others are expected to implement
To declare the interface to an object while concealing its class
To capture similarities among classes that are not hierarchically related

read more uses of protocols.

阅读更多协议的用法。

#3


2  

Protocols work like interfaces in Java. You can define protocol with some methods and then implement it in many classes. Each of these classes could have different implementation of methods provided by protocol, but the interface would be the same. For example: you want to calculate salary of different types of employees, ex. FullTimeEmployee and ContractEmployee. You can put calculateSalary method definition in protocol and provide different implementations in these two classes, ex. one would return fixed salary while the other would return salary based on worked hours. Then you can cast each class instance to protocol type and invoke given method receiving appropriate results.

协议的工作方式类似于Java中的接口。您可以用一些方法定义协议,然后在许多类中实现它。每个类都可以有协议提供的方法的不同实现,但是接口是相同的。例如:你想计算不同类型员工的工资,例如全职员工和合同工。您可以将calculateSalary方法定义放在协议中,并在这两个类中提供不同的实现,例如,一个返回固定工资,另一个返回基于工作时间的工资。然后可以将每个类实例转换为协议类型,并调用接收适当结果的给定方法。

Categories are helpful if you want to extend a class which doesn't belong to you, like in case of built-in classes, ex. NSString. You can add a method like reverse to get reversed string. Why not to make a subclass? Because you can call your custom method on modified class without casting reference to your type.

如果您想扩展一个不属于您的类,类别是很有帮助的,比如对于内置类,例如NSString。您可以添加像reverse这样的方法来获得反向字符串。为什么不创建一个子类呢?因为您可以在修改后的类上调用自定义方法,而不必对类型进行强制引用。

#4


2  

Protocols are a way of applying a code contract to a class, in much the same way that interfaces work in Java and C#.

协议是将代码契约应用到类的一种方式,其方式与Java和c#中的接口非常相似。

Categories are useful to extend classes that you cannot directly modify, they are very similar to Extension methods in C#.

类别对于扩展无法直接修改的类非常有用,它们与c#中的扩展方法非常相似。

#5


1  

Categories can be used to add functionality to existing classes, such as all the NS... classes. You can not add code to these directly.

类别可以用于向现有类添加功能,比如所有的NS…类。您不能直接向它们添加代码。

Protocols are used to specify that a certain class actually does something, when you call a method in the protocol.

当您在协议中调用一个方法时,协议被用来指定某个类实际上做了一些事情。

#1


15  

Categories are to add methods to classes whose source is unavailable to you, such as all the Apple classes (those starting with NS, CG, CA, etc.), without needing to subclass them.

类别是向那些源代码不可用的类添加方法,例如所有Apple类(以NS、CG、CA等开头的类),而不需要对它们进行子类化。

The purpose of protocols is to define methods that classes adhering to that protocol have to implement. In Java, those are called interfaces. The purpose is to codify similarities between classes that are not siblings (subclasses of the same superclass). Suppose you have a class Chair and a class Petrol. These don't have a lot in common, except that they both adhere to the flammable protocol, which requires them to have certain methods, such as specificEnergy and flamingPoint.

协议的目的是定义遵循该协议的类必须实现的方法。在Java中,这些被称为接口。目的是将非兄弟类(同一个超类的子类)之间的相似之处归类。假设你有一把椅子和一把汽油。它们之间没有很多共同点,只是它们都遵守可燃协议,这就要求它们有特定的方法,比如特定的能量和火焰点。

Now your Fire class can have a method addFlammableMaterial:(id <flammable>)material.

现在您的消防类可以有一个方法添加易燃材料:(id <易燃> )材料。

Protocols are often used to declare that instances of certain classes can be delegates for certain other instances. You can declare your view controller to act as the data source to a UITableView by declaring it to conform to the UITableViewDataSource protocol, which means your viewController guarantees that it implements the required methods of that protocol, and the tableView can rest safely because it can trust the VC to be its data source.

协议通常用于声明某些类的实例可以为某些其他实例的委托。你可以声明视图控制器作为数据源的UITableView声明符合需要的协议,这意味着你viewController保证它实现了协议的要求的方法,和表视图可以休息安全,因为它可以信任VC数据源。

#2


4  

A category is a way of adding new methods to all instances of an existing class without modifying the class itself.

类别是一种向现有类的所有实例添加新方法的方法,而无需修改类本身。

For category read the already discussed SO post,

对于类别阅读已经讨论过的文章,

What is the difference between inheritance and Categories in Objective-C

在Objective-C中继承和类别的区别是什么

protocol-versus-category

protocol-versus-category

Protocols are used frequently to specify the interface for delegate objects.

协议经常用于指定委托对象的接口。

Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:

协议声明可以被任何类实现的方法。协议在至少三种情况下是有用的:

To declare methods that others are expected to implement
To declare the interface to an object while concealing its class
To capture similarities among classes that are not hierarchically related

read more uses of protocols.

阅读更多协议的用法。

#3


2  

Protocols work like interfaces in Java. You can define protocol with some methods and then implement it in many classes. Each of these classes could have different implementation of methods provided by protocol, but the interface would be the same. For example: you want to calculate salary of different types of employees, ex. FullTimeEmployee and ContractEmployee. You can put calculateSalary method definition in protocol and provide different implementations in these two classes, ex. one would return fixed salary while the other would return salary based on worked hours. Then you can cast each class instance to protocol type and invoke given method receiving appropriate results.

协议的工作方式类似于Java中的接口。您可以用一些方法定义协议,然后在许多类中实现它。每个类都可以有协议提供的方法的不同实现,但是接口是相同的。例如:你想计算不同类型员工的工资,例如全职员工和合同工。您可以将calculateSalary方法定义放在协议中,并在这两个类中提供不同的实现,例如,一个返回固定工资,另一个返回基于工作时间的工资。然后可以将每个类实例转换为协议类型,并调用接收适当结果的给定方法。

Categories are helpful if you want to extend a class which doesn't belong to you, like in case of built-in classes, ex. NSString. You can add a method like reverse to get reversed string. Why not to make a subclass? Because you can call your custom method on modified class without casting reference to your type.

如果您想扩展一个不属于您的类,类别是很有帮助的,比如对于内置类,例如NSString。您可以添加像reverse这样的方法来获得反向字符串。为什么不创建一个子类呢?因为您可以在修改后的类上调用自定义方法,而不必对类型进行强制引用。

#4


2  

Protocols are a way of applying a code contract to a class, in much the same way that interfaces work in Java and C#.

协议是将代码契约应用到类的一种方式,其方式与Java和c#中的接口非常相似。

Categories are useful to extend classes that you cannot directly modify, they are very similar to Extension methods in C#.

类别对于扩展无法直接修改的类非常有用,它们与c#中的扩展方法非常相似。

#5


1  

Categories can be used to add functionality to existing classes, such as all the NS... classes. You can not add code to these directly.

类别可以用于向现有类添加功能,比如所有的NS…类。您不能直接向它们添加代码。

Protocols are used to specify that a certain class actually does something, when you call a method in the protocol.

当您在协议中调用一个方法时,协议被用来指定某个类实际上做了一些事情。