Objective-C Tutorials

h文件

#import "AnyHeaderFile.h"
@interface ClassName : SuperClass
{
@property int x; // @property属性成员, m文件不用自己写getter 和 setter
@property (nonatomic, assign) int commentTimeFontSize;
}
// 公有方法
+ (void) getInstance; // 静态方法
- (type)doItWithA:(type)a;
- (type)doItWithA:(type)a
ParamB:(type)b
ParamC:(type)c;
@end

m文件

#import "YourClassName.h"
@interface ClassName ()
// m文件里也可以定义interface
@end
@implementation ClassName {
// define private instance variables
}
// implement methods
@end

对象

// 实例化
ClassName * myObject =[[ClassName alloc] init]; // init可以认作是构造函数

// 方法调用
[myObject doIt];
[myObject doItWithA:a];
[myObject doItWithA:a ParamB:b];

// 属性 @property
[myObject setPropertyName:a];
myObject.propertyName = a; // alt
a = [myObject propertyName];
a = myObject.propertyName; // alt

NSString

NSString *personOne = @"Ray";
NSString *personTwo = @"Shawn";
NSString *combinedString = [NSString stringWithFormat:@"%@: Hello, %@!", personOne, personTwo];
NSLog(@"%@", combinedString);

NSString *tipString = @"24.99";
float tipFloat = [tipString floatValue];

NSArray

NSMutableArray *array = [@[person1, person2] mutableCopy];
[array addObject:@"Waldo"];
NSLog(@"%d items!", [array count]);

for (NSString *person in array) {
NSLog(@"Person: %@", person);
}
NSString *waldo = array[2];

Block语法

// 定义一个代码块
^{
NSLog(@"This is a block");
}

如何使用:

void (^simpleBlock)(void); // 相当于函数指针
simpleBlock = ^{
NSLog(@"This is a block");
};

参考