简体   繁体   English

Xcode添加objective-c文件“文件类型”

[英]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" 我正在尝试使用Xcode 6.1.1向我的Xcode项目添加一个新文件,Xcode现在有一个“文件类型”选项,您可以在其中选择“空文件,类别,协议,扩展”

Can someone explain the differences between these and what the default to select would be? 有人可以解释这些与默认选择之间的差异吗? My file is a subclass of NSObject. 我的文件是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. 虽然类别允许您覆盖方法,但Objective-C无法确定应该使用哪个方法定义,因此您永远不应该使用类别来覆盖方法。 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." 类别可以包含受保护的方法,“通过简单地导入类别,允许任意文件'选择加入'到API的一部分。” (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. 扩展提供与类别类似的功能,但必须在主实现文件中实现扩展的API。

Extensions can also be used to create a formal private API. 扩展也可用于创建正式的私有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. 使用扩展,您可以在.m文件的接口和实现块中定义私有方法。 As long as you do not include it in the respective .h file, these methods will be treated as private methods. 只要您不将它包含在相应的.h文件中,这些方法将被视为私有方法。

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. 协议由一个API组成,可以由各种类使用,无论它们是否相关。 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 为了使用协议,类只需要:1。在类/超类名称声明之后将协议的名称包含在有角度的括号<>中2.实现协议的方法

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 . 资料来源: RyPress关于类别和扩展的 文章关于协议的RyPress文章 Both articles have helpful examples of each tool. 这两篇文章都有每个工具的有用示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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