iPhone开发基础(第2部分)(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/iphone-development-basics-part-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1644 个词 阅读量 0iPhone开发基础(第2部分)(译文)
原文地址:https://www.codeproject.com/Articles/231841/IPhone-Development-Basics-Part
原文作者:Rashid Malik Awan
译文由本站 robot-v1.0 翻译
前言
Exploring the Objective-C File Structure
探索Objective-C文件结构
头文件/接口文件(Header/Interface Files)
创建类会创建两个不同的文件:接口(或头)文件((Creating a class creates two different files: an interface (or header) file ().H(.h))和实现文件(() and an implementation file ().m(.m)).接口文件用于定义类将使用的所有方法和属性的列表.另一方面,在实现文件中,您将编写使头文件中定义的所有内容正常工作的代码.(). The interface file is used to define a list of all of the methods and properties that your class will be using. The implementation file, on the other hand, is where you’ll go to write the code that makes everything defined in the header file work.)
的(The) #import
指令:指令是添加到文件中的命令,可帮助Xcode及其相关工具构建应用程序.(directive: Directives are commands that are added to your files that help Xcode and its associated tools build your application.)
的(The) @interface
指令和实例变量:第3行使用(directive and Instance variables: Line 3 uses the) @interface
指令以开始一组行(包含在(directive to begin a set of lines (enclosed in) {}
花括号)描述类将提供的所有实例变量.(braces) to describe all the instance variables that your class will be providing.)
什么是协议(What’s a Protocol):协议是Objective-C的独特功能,听起来很复杂,但实际上并非如此.有时,您会遇到一些功能,这些功能要求您编写支持其使用的方法,例如提供要在表中显示的项目列表.您需要编写的方法以通用名称(称为"协议")分组在一起.(: Protocols are a unique feature of Objective-C that sound complicated but really aren’t. Sometimes, you will come across features that require you to write methods to support their use—such as providing a list of items to be displayed in a table. The methods that you need to write are grouped together under a common name—this is known as a “protocol.”) 定义方法(Defining Methods):第8行和第10行声明了需要在该类中实现的两个方法:(: Lines 8 and 10 declare two methods that need to be implemented in the class:)
的(The) @property
指令:(directive: The) @property
指令与实现文件中的另一个命令synthesize结合使用,以简化您与您在界面中定义的实例变量进行交互的方式.(directive is used in conjunction with another command called syn- thesize in the implementation file to simplify how you interact with the instance variables that you’ve defined in your interface.)
结束接口文件:要结束接口文件,请添加(Ending the Interface File: To end the interface file, add) @end
在自己的路线上.可以在示例文件的第14行看到:(on its own line. This can be seen on line 14 of our example file:)
实施文件(Implementation Files)
在接口文件中定义实例变量(或属性!)和方法之后,您需要完成编写代码的工作以实现应用程序的逻辑.实施文件((After you’ve defined your instance variables (or properties!) and methods in your interface file, you need to do the work of writing code to implement the logic of your application. The implementation file ().m(.m))包含使您的课堂正常工作的所有"资料".() holds all of the “stuff” that makes your class work.)
的(The) #import
指令:(directive: The) #import
指令通过导入与该类关联的接口文件开始第1行的工作:(directive kicks things off in line 1 by importing the interface file associated with the class:)
的(The) @implementation
指令:(directive: The) implementation
第3行中显示的指令告诉Xcode文件将要实现的类.(directive, shown in line 3, tells Xcode what class the file is going to be implementing.)
的(The) @synthesize
指令:在第5行中,我们使用(directive: In line 5, we use the) @synthesize
指令在幕后为实例变量的getter和setter生成代码:(directive to, behind the scenes, generate the code for the getters and setters of an instance variable:)
方法实施(Method Implementation):实现文件必须重述方法的定义,但是在末尾添加了一组花括号{},而不是用分号(;)结束,如第7-9和11-13行所示.您的编程的所有魔力将在以下括号之间发生:(: The implementation file must restate the method definitions, but, rather than ending them with a semicolon (;), a set of curly braces, {}, is added at the end, as shown in lines 7–9 and 11–13. All the magic of your programming will take place between these braces:)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
iPhone 新闻 翻译