ios developer tiny share-20160811

今天讲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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值