Objective-C初步研究 - 接口文件(interface file)

1. Exploring the Objective-C File Structure

    建立一个Objective-C的类会新建两个文件, 一个接口文件(头文件)(interface file), 后缀为.h,

    一个实现文件(implementation file), 后缀为.m

    (见下图)

 

   顾名思义, 接口文件定义所有方法签名, 实现文件具体实现代码逻辑

   看下面这段代码

 

 

#import语句用来导入某个头文件, 学过Java的朋友可以知道, 它类似Java中的import语句, 而Java中是导入某个包

 

Directive(指示语句)

Directives are commands that are added to your files that help Xcode and its
associated tools build your application.
 

(Directives你可以把它看成一种命令, 类似.NET中的@指示, 它让Xcode编译器知道你的代码如何组织)

 

@interface myClass : myParent <myProtocol> {
  NSString *myString;
  IBOutlet UILabel *myLabel;
}

 

Protocol(协议)

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.”

(你可以把它想像成C#或者Java中的接口, 协议中定义了哪些方法, 你就需要去实现这些方法)

 

 

+ (NSString)myClassMethod:(NSString)aString;
- (NSDate)myInstanceMethod:(NSString)aString anotherParameter:(NSURL)aURL;

 

The + denotes a class method(+号表示该方法是一个类方法)

- indicates an instance method(-号表示该方法是一个实例方法)

 

 

@property指示

 

@property (nonatomic, retain) UILabel *myLabel;

 

如果程序要访问实例变量(instance variable), 如果你没有加入该指示, 必须使用get, set方法

你可以把该指示想像成.NET中的属性

public  string PropertyName {  getset; }

 

原来使用get, set访问的代码

 

[myLabel setText:@”Hello World”];

theCurrentLabel=[myLabel getText];

 

 加了属性指示后, 我们可以像下面这样简单的访问

 

myLabel.text=@”Hello World”;
theCurrentLabel=myLabel.text;

 

 这样就方便了很多

 

最后, 接口结束必须以结束指示符

@end

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值