category、extension、protocol
NSObject+print.h
#import <Foundation/Foundation.h>
@interface NSObject (print)
- (void)print;
@end
NSObject+print.m
#import "NSObject+print.h"
@implementation NSObject (print)
- (void)print
{
NSLog(@"%@", self);
}
@end
NSDate+Transfer.h
#import <Foundation/Foundation.h>
@interface NSDate (Transfer)
+ (NSDate *)dateWithDateString:(NSString *)str;
@end
NSDate+Transfer.m
#import "NSDate+Transfer.h"
@implementation NSDate (Transfer)
+ (NSDate *)dateWithDateString:(NSString *)str;
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
return [[formatter dateFromString:str] dateByAddingTimeInterval:8 * 60 * 60];
}
@end
NSString+Other.h
#import <Foundation/Foundation.h>
@interface NSString (Other)
+ (void)luoBen;
@end
NSString+Other.m
#import "NSString+Other.h"
@implementation NSString (Other)
+ (void)luoBen
{
NSLog(@"xx在广阔的草原上,发疯似的狂奔");
}
@end
Student.h
// 在当前类的父类名后,添加 <...> ,内部写协议名称,就代表遵守了该协议,相当于拥有了该协议中所有方法的声明
@interface Student : NSObject <StudyingProtocol>
- (void)sayHi;
- (void)sayHello;
@end
Student.m
#import "Student.h"
@interface Student ()
- (void)daoZhangWenHao;
@end
@implementation Student
- (void)daoZhangWenHao
{
NSLog(@"我是道长,我向大家问好。。。");
}
- (void)sayHi
{
NSLog(@"Hi");
[self daoZhangWenHao];
}
- (void)sayHello
{
NSLog(@"Hello");
[self daoZhangWenHao];
}
- (void)goodStudying
{
NSLog(@"好好学习,天天向上");
}
- (void)doHomework
{
NSLog(@"认真做作业");
}
- (void)xiaoFeng
{
NSLog(@"我们需要校风。。。");
}
@end
StudyingProtocol.h
#import <Foundation/Foundation.h>
@protocol StudyingProtocol <NSObject>
@required
- (void)goodStudying;
- (void)doHomework;
- (void)xiaoFeng;
@optional
- (void)needWater;
- (void)neediPhone;
@end
main.m
#import <Foundation/Foundation.h>
#import "NSString+Other.h"
#import "NSDate+Transfer.h"
#import "NSObject+print.h"
#import "Student.h"
typedef CGFloat (^maxAnsBlock) (CGFloat, CGFloat);
int main(int argc, const char * argv[]) {
@autoreleasepool {
CGFloat (^maxBlock) (CGFloat, CGFloat) = ^(CGFloat f1, CGFloat f2) {
return f1 > f2 ? f1 : f2;
};
CGFloat max = maxBlock(23.8, 12.5);
NSLog(@"%.2f", max);
maxAnsBlock max0 = ^(CGFloat a, CGFloat b) {
return a > b ? a : b;
};
CGFloat ans = max0(34.6, 23.0);
NSLog(@"%.2f", ans);
NSDate *nowDate = [[NSDate alloc] init];
NSLog(@"nowDate = %@", nowDate);
NSDate *tomorrowThisDate = [nowDate dateByAddingTimeInterval:24 * 60 * 60];
NSLog(@"tomorrowThisDate = %@", tomorrowThisDate);
NSDate *yesterdayThisDate = [NSDate dateWithTimeInterval:-(24 * 60 * 60) sinceDate:nowDate];
NSLog(@"yesterdayThisDate = %@", yesterdayThisDate);
NSDate *afterNowDate = [NSDate dateWithTimeIntervalSinceNow:1500];
NSLog(@"afterNowDate = %@", afterNowDate);
NSTimeInterval secondInterval = [tomorrowThisDate timeIntervalSinceDate:yesterdayThisDate];
NSLog(@"secondInterval = %.2f", secondInterval);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss eee"];
NSString *nowDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"时间格式:%@", nowDateStr);
[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss eee"];
NSDate *nowDate0 = [dateFormatter dateFromString:nowDateStr];
NSLog(@"时间格式:%@", nowDate0);
NSString *strDate = @"2015年05月25日 10点23分20秒";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
NSDate *date0 = [formatter dateFromString:strDate];
NSLog(@"时间格式:%@", date0);
[NSString luoBen];
NSString *str = @"20140402142850";
NSLog(@"%@", [NSDate dateWithDateString:str]);
NSArray *array = [NSArray arrayWithObjects:@"key", @"mon", nil];
[array print];
}
return 0;
}