您的位置:首页 > 移动开发 > IOS开发

ios developer tiny share-20160811

2016-08-11 17:08 281 查看
今天讲Objective-C的实现一个接口的语法,以及.h、.m文件,以及#import。我们会通过一个例子来学习这些语法概念。另外,还会顺带讲下Objective-C的代码规范,如camel命名方法名,缩进等。

The Implementation of a Class Provides Its Internal Behavior

Once you’ve defined the interface for a class, including the properties and methods intended for public access, you need to write the code to implement the class behavior.

As stated earlier, the interface for a class is usually placed inside a dedicated file, often referred to as a header file, which generally has the filename extension .h. You write the implementation for an Objective-C class inside a source code file with the
extension .m.

Whenever the interface is defined in a header file, you’ll need to tell the compiler to read it before trying to compile the implementation in the source code file. Objective-C provides a preprocessor directive, #import, for this purpose. It’s similar to the
C #include directive, but makes sure that a file is only included once during compilation.

Note that preprocessor directives are different from traditional C statements and do not use a terminating semi-colon.

Basic Syntax

The basic syntax to provide the implementation for a class looks like this:

#import "XYZPerson.h"

@implementation XYZPerson

@end
Implementing Methods

For a simple class interface with one method, like this:
@interface XYZPerson : NSObject
- (void)sayHello;
@end

the implementation might look like this:
#import "XYZPerson.h"

@implementation XYZPerson
- (void)sayHello {
NSLog(@"Hello, World!");
}
@end

This example uses the NSLog() function to log a message to the console. It’s similar to the standard C library printf() function, and takes a variable number of parameters, the first of which must be an Objective-C string.

Method implementations are similar to C function definitions in that they use braces to contain the relevant code. Furthermore, the name of the method must be identical to its prototype, and the parameter and return types must match exactly.

Objective-C inherits case sensitivity from C, so this method:
- (void)sayhello {
}

would be treated by the compiler as completely different to the sayHello method shown earlier.

In general, method names should begin with a lowercase letter. The Objective-C convention is to use more descriptive names for methods than you might see used for typical C functions. If a method name involves multiple words, use camel case (capitalizing the
first letter of each new word) to make them easy to read.

Note also that whitespace is flexible in Objective-C. It’s customary to indent each line inside any block of code using either tabs or spaces, and you’ll often see the opening left brace on a separate line, like this:
- (void)sayHello
{
NSLog(@"Hello, World!");
}

Xcode, Apple’s integrated development environment (IDE) for creating OS X and iOS software, will automatically indent your code based on a set of customizable user preferences. See Changing the Indent and Tab Width in Xcode Workspace Guide for more information.

You’ll see many more examples of method implementations in the next chapter, Working with Objects.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息