初学Objective-C,会很不习惯,本文介绍该语言的基本语法知识。
特别符号的说明:
-减号,表示一个函数、或者方法、或者消息的开始。- 实例方法,由类的实例来执行。换言之,在调用实例方法之前,必须先创建该类的实例。实例方法是最常见的方法类型。
-(void) hello:(BOOL)ishello -(函数返回值类型)函数名:(参数1类型)参数1名
{
//OOXX
}
-减号,表示一个函数、或者方法、或者消息的开始。- 实例方法,由类的实例来执行。换言之,在调用实例方法之前,必须先创建该类的实例。实例方法是最常见的方法类型。
-(void) hello:(BOOL)ishello -(函数返回值类型)函数名:(参数1类型)参数1名
{
//OOXX
}
+加号,后面也是跟函数,相当于表示c++的类的静态方法。+ 类方法,可由它所在的类直接执行。它不需要对象的实例作为消息的接收者,
[ ]中括号,是调用类的方法,通常在Objective-C里说“消息”。
[self hello:YES]; self是类名,hello是方法,YES是实参。
:冒号,一般来讲,冒号左边是参数名或方法名,冒号右边是参数值名。
函数的第一个参数没有参数名,所以第一个冒号的左边是方法名。
函数的第二个参数开始就有参数名了,冒号的左边是参数名。
@符号,
"字符串"是C的字符串, @""是把C的字符串转成NSString的一个简写.
在需要NSString的地方才需要这个转化,例如NSLog里面.
在需要NSString的地方才需要这个转化,例如NSLog里面.
在需要C string的地方,还是用"字符串"的.
另外,@”"这个转换是不支持中文的.例如NSLog(@”字符串”); 是一定输出不了中文的.
.点运算符,用于访问对象的某个成员变量:
The dot syntax should only be used setters and getters, not for general purpose methods.
定义一个类:
在h文件编写类的接口:The class interface is usually stored in the ClassName.h file, and defines instance variables and public methods.
在m文件编写类的实现:The implementation is in the ClassName.m file and contains the actual code for these methods. It also often defines private methods
that aren't available to clients of the class.
类的接口示例: 中括号里包上类的成员,后面-/+号跟着类的方法。
@interface Photo : NSObject //所有类都要继承NSObject
{ //花括号括起来类的成员
NSString* caption;
NSString* photographer;
}
- (NSString*) caption; //花括号外是类的各个方法
- (NSString*) photographer;
- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;
NSString* caption;
NSString* photographer;
}
- (NSString*) caption; //花括号外是类的各个方法
- (NSString*) photographer;
- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;
@end
类的实现示例: 把构造函数、析构函数、自定义函数实现掉。
@implementation Photo
- (NSString*) caption { //这是一个getter
return caption;
}
- (NSString*) photographer {
return photographer;
- (NSString*) caption { //这是一个getter
return caption;
}
- (NSString*) photographer {
return photographer;
}
- (void) setCaption: (NSString*)input
{
[caption autorelease];
caption = [input retain];
}
- (void) setPhotographer: (NSString*)input
{
[photographer autorelease];
photographer = [input retain];
{
[caption autorelease];
caption = [input retain];
}
- (void) setPhotographer: (NSString*)input
{
[photographer autorelease];
photographer = [input retain];
}
- (id) init //相当于构造函数
{
if ( self = [super init] ) //先构造父类对象
{
[self setCaption:@"Default Caption"];
[self setPhotographer:@"Default Photographer"];
}
return self;
{
if ( self = [super init] ) //先构造父类对象
{
[self setCaption:@"Default Caption"];
[self setPhotographer:@"Default Photographer"];
}
return self;
}
- (void) dealloc //相当于析构函数,它会自动隐式调用
{
[caption release];
[photographer release];
[super dealloc]; //释放父类对象
}
{
[caption release];
[photographer release];
[super dealloc]; //释放父类对象
}
@end
解释一下这一句self = [super init]:
This is a single equals sign, which assigns the result of [super init] to self. This essentially just asks the superclass to do its own initialization.
创建对象有两种方法:
NSString* myString = [NSString string]; 自动创建
NSString* myString = [[NSString alloc] init]; 手工创建
// string1 will be released automatically
NSString* string1 = [NSString string];
// 用完必须手工释放掉
NSString* string2 = [[NSString alloc] init];
NSString* string1 = [NSString string];
// 用完必须手工释放掉
NSString* string2 = [[NSString alloc] init];
[string2 release];
类的函数有两种类型:
call method on object 应该是减号定义 instance method
call method on class 应该是加号定义 静态方法 class method
函数调用的方式:
无参数函数
output = [object methodWithOutput];
有参数函数
output = [object methodWithInputAndOutput:input];函数定义的方式:
多参数函数的定义 input method looks like this:
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
多参数函数的调用 You call the method like this:
BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
多参数函数的调用 You call the method like this:
BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
Nested Messages是嵌套调用的方法。Whenever you see code inside square brackets, you are sending a message to an object or a class.