简体   繁体   中英

Xcode adding objective-c file “file type”

I'm trying to add a new file to my Xcode project using Xcode 6.1.1 and Xcode now has a "File type" option where you select between "Empty File, Category, Protocol, Extension"

Can someone explain the differences between these and what the default to select would be? My file is a subclass of NSObject.

Thanks

Category

Categories are used to help modularize and organize class definitions. They allow you to take a (complex) class definition and spread it over several organized classes. It is not the same as subclassing. Although categories do allow you to override methods, Objective-C has no way of determining 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 per usual.

Categories can contain protected methods, which "allow arbitrary files to 'opt-in' to a portion of an API by simply importing the category." (Check out the articles linked below.)

Extension

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

Extensions can also be used to create a formal private API. Ordinarily, if you wanted to create private methods, you would write them in the implementation block, but would exclude them from the interface block. However, if you have an extensive group of methods that you would like to remain private, this becomes cumbersome and difficult to read/maintain. Using extensions, you can define the private methods in both the interface and implementation blocks of the .m file. As long as you do not include it in the respective .h file, these methods will be treated as private methods.

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

Protocol

Protocols allow for abstracted horizontal relationships across various (sometimes unrelated) classes and class hierarchies. A protocol consists of an API that can be used by a variety of classes, regardless of whether or not they are related. This allows you to modify/add some class functionality through a potentially wide range of classes without having to subclass them and alter their own class hierarchies.

In order to use a protocol, a class only needs to: 1. Include the protocol's name inside angled brackets <> after the class/superclass name declaration 2. Implement the protocol's methods

Protocols can also be useful for type checking.

Empty File

An empty file is just that - an empty file. You give it a name, but it contains no class information whatsoever (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM