1、传递实参的消息
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate *now = [NSDate date];
NSLog(@"This NSDate object lives at %p",now);
NSLog(@"The date is %@",now);
double seconds = [now timeIntervalSince1970];
NSLog(@"It has been %f seconds since the start of 1970.",seconds);
//这里调用了dateByAddingTimeInterval:方法,并传递了实参100000. 方法带实参的时候,冒号
//是构成方法名的一个重要部分。
NSDate *later = [now dateByAddingTimeInterval:100000];
NSLog(@"In 100000 seconds it will be %@",later);
}
return 0;
}
2、多个参数
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate *now = [NSDate date];
NSLog(@"This NSDate object lives at %p",now);
NSLog(@"The date is %@",now);
double seconds = [now timeIntervalSince1970];
NSLog(@"It has been %f seconds since the start of 1970.",seconds);
//
NSDate *later = [now dateByAddingTimeInterval:100000];
NSLog(@"In 100000 seconds it will be %@",later);
//该方法用来获取NSCalendar实例,NSCalendar常被用来获取小时,日期,周,月等信息。
NSCalendar *cal = [NSCalendar currentCalendar];
NSLog(@"My calendar is %@", [cal calendarIdentifier]);
//该方法用来获取now是本月的第几天。传入三个参数。前两个是NSCalendar类的常量,描述我们希望
//得到信息的类型。第三个是NSDate对象,表示要得到哪个对象的信息。
unsigned long day = [cal ordinalityOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitMonth
forDate:now];
NSLog(@"This is day %lu of the month",day);
}
return 0;
}
3、alloc和init
alloc用来创建对象,init用来进行初始化实例。
例如: NSDate *now = [[NSDate alloc] init];
该种方式创建和发送date消息创建NSDate对象没有区别。
4、向nil发送消息
nil: 表示不指向任何对象的指针。
OC中可以向nil 发送消息,但是什么事情也不会发生。
5、id
id是一种类型,可以指向任意类型OC对象的指针。
如:id delegate;
id已经隐含了星号的作用。