Xcode adds objective-c file "file type"

I am trying to add a new file to my Xcode project using Xcode 6.1.1 and Xcode now has the "File Type" option where you select "Empty File, Category, Protocol, Extension"

Can someone explain the differences between the two and what will be selected by default? My file is a subclass of NSObject.

thank

+3


source to share


1 answer


Category

Categories are used to facilitate modulation and ordering of class definitions. They allow you to take a (complex) class definition and extend it to multiple organized classes. This is not the same as subclasses. Although categories allow you to override methods, Objective-C has no way to determine which method definition should be used, so you should never use a category to override methods. Instead, create a subclass that overrides the method as usual.

Categories can contain protected methods that "allow arbitrary files to" enter "part of the API by simply importing the category." (Check out the articles below.)

Expansion

Extensions provide similar functionality for categories, except that you must implement the extension API in the main implementation file.

Extensions can also be used to create a formal private API. Usually, if you want to create private methods, you must write them in the implementation block, but exclude them from the interface block. However, if you have a large group of methods that you would like to remain confidential, it becomes cumbersome and difficult to read / maintain. Using extensions, you can define private methods both in the interface and in the implementation blocks of the .m file. As long as you don't include it in the appropriate .h file, these methods will be treated as private methods.

Extensions can also be used to make previously declared properties read-only outside the read-write class within the class (using the "self." Syntax).



Protocol

Protocols allow you to abstract horizontal relationships between different (sometimes unrelated) classes and class hierarchies. The protocol consists of an API that can be used by various classes, whether they are related or not. This allows you to modify / add some of the functionality of a class with a potentially wide range of classes without having to subclass them and change your own class hierarchies.

To use the protocol, a class only needs to: 1. Include the protocol name inside angle brackets <> after the class / superclass name declaration 2. Implement the protocol methods

Protocols can also be useful for type checking.

Empty file

An empty file is an empty file. You give it a name, but it doesn't contain any information about the class (no generated methods, blocks, comments, etc.).

Sources: RyPress Article on Categories and Extensions and RyPress Article on Protocols . Both articles have helpful examples of each tool.

+2


source







All Articles