8、category、extension、protocol

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
#import <Foundation/Foundation.h>
#import "StudyingProtocol.h"

// 在当前类的父类名后,添加 <...> ,内部写协议名称,就代表遵守了该协议,相当于拥有了该协议中所有方法的声明
@interface Student : NSObject <StudyingProtocol>

- (void)sayHi;
- (void)sayHello;

@end
Student.m
#import "Student.h"

// 私有方法声明放在Student.m的延展中,在Student.m中去实现私有方法,然后只能在内部使用
@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 {


        // 求两个浮点数的最大值,用block
        CGFloat (^maxBlock) (CGFloat, CGFloat) = ^(CGFloat f1, CGFloat f2) {
            return f1 > f2 ? f1 : f2;
        };
        CGFloat max = maxBlock(23.8, 12.5);
        NSLog(@"%.2f", max);

        // 使用block重定义的别名
        maxAnsBlock max0 = ^(CGFloat a, CGFloat b) {
            return a > b ? a : b;
        };
        CGFloat ans = max0(34.6, 23.0);
        NSLog(@"%.2f", ans);



        // 1.NSDate
        // eg1、使用初始化方法,创建一个当前时间对象
        NSDate *nowDate = [[NSDate alloc] init];
        NSLog(@"nowDate = %@", nowDate);

        // eg2、在当前时间基础上,添加一天,得到明天这个时候的时间
        NSDate *tomorrowThisDate = [nowDate dateByAddingTimeInterval:24 * 60 * 60];
        NSLog(@"tomorrowThisDate = %@", tomorrowThisDate);

        // eg3、在当前时间基础上,减去一天,得到昨天这个时候的时间
        NSDate *yesterdayThisDate = [NSDate dateWithTimeInterval:-(24 * 60 * 60) sinceDate:nowDate];
        NSLog(@"yesterdayThisDate = %@", yesterdayThisDate);

        // eg4、用便利构造器,创建一个距离现在1500秒之后的时间
        NSDate *afterNowDate = [NSDate dateWithTimeIntervalSinceNow:1500];
        NSLog(@"afterNowDate = %@", afterNowDate);

        // eg5、获取明天和昨天之间相隔的秒数
        NSTimeInterval secondInterval = [tomorrowThisDate timeIntervalSinceDate:yesterdayThisDate];
        NSLog(@"secondInterval = %.2f", secondInterval);


        // 2.NSDateFormatter
        // 创建时间转换的对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        // 设置时间格式串格式
        [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss eee"];   // eee代表“周一”、eeee代表“星期一”

        // 将NSDate转换为NSString
        NSString *nowDateStr = [dateFormatter stringFromDate:[NSDate date]];
        NSLog(@"时间格式:%@", nowDateStr);

        // 将NSString转换为NSDate
        [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss eee"];
        NSDate *nowDate0 = [dateFormatter dateFromString:nowDateStr];   // 转换后没有格式
        NSLog(@"时间格式:%@", nowDate0);

        // ----- eg.2 -----
        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);



        // 3.Category,不能添加实例变量,文件命名:原始类名+类目名
        // 如果通过Category添加的方法和系统的方法重复:(尽可能不要重复)
        // + 方法,后添加的方法优先级高
        // - 方法,系统自带的方法优先级高
        [NSString luoBen];

        NSString *str = @"20140402142850";
        NSLog(@"%@", [NSDate dateWithDateString:str]);


        NSArray *array = [NSArray arrayWithObjects:@"key", @"mon", nil];
        [array print];


        // 4.Extension

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值