Objective-c - Purpose of categories and protocols

后端 未结 5 1053
余生分开走
余生分开走 2020-12-15 01:50

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 ins

相关标签:
5条回答
  • 2020-12-15 02:13

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

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

    0 讨论(0)
  • 2020-12-15 02:26

    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.

    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.

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

    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.

    0 讨论(0)
  • 2020-12-15 02:26

    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.

    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.

    0 讨论(0)
  • 2020-12-15 02:29

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

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

    0 讨论(0)
  • 2020-12-15 02:30

    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

    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.

    0 讨论(0)
提交回复
热议问题