OC: 类的扩展、类的延展、协议、 NSDate

 

 

NSDateFormatter

指定⽇日期格式:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

⽇日期转化为字符串:
NSDateFormatter*formatter =
[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *date
String=[formatter stringFromDate: [NSDate date]]; 

 

NSDate 处理日期或者时间的一个类,用于获取日期

        NSDate * date = [NSDate date];//是个类方法

全球份24个时区,我们控制台打印的是0时区的时间

        //获取当前的日期时间

        NSDate * date = [NSDate date];//是个类方法全球份24个时区,我们控制台打印的是0时区的时间

        NSLog(@"%@",date);

        //获取明天此时的时间 ,计算明天距离此时的时间,以及本单位秒来计算的

        NSDate * date1 = [NSDate dateWithTimeIntervalSinceNow:24*60*60];

        NSLog(@"tomorrow = %@",date1);

 //获取昨天的日期

        NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];

        NSLog(@"yesterday = %@",yesterday);

 

dateWithTimeIntervalSinceNow方法

        //获取1970.1.1到此时的时间间隔

        NSTimeInterval timeInterval = [datenow timeIntervalSince1970];

        NSLog(@"%f",timeInterval);//结果是以秒为单位

//获取两个日期的时间间隔

        NSTimeInterval interval = [datenow timeIntervalSinceDate:tomorrow];//用前面日期 - 后面的日期 datenow - tomorrow)以秒为单位

        NSLog(@"%f",interval);

        NSLog(@"获取两个日期的时间间隔 %.1f ",interval/24/60/60);//计算相差天数

        

        //获取据当前日期的时间间隔

        NSTimeInterval currentInterval = [yesterday timeIntervalSinceNow];

        NSLog(@"据当前日期的时间间隔 %.1f ",currentInterval/24/60/60);

        

        //两个日期之间的比较(返回一个枚举值)默认是升序比较

        NSComparisonResult result = [yesterday compare:tomorrow];

        NSLog(@"%ld",result);

        

        //判断两个日期是否相等(返回BOOL)

        BOOL isEqual = [tomorrow isEqualToDate:yesterday];

        if (isEqual) {

            NSLog(@"两个日期相等");

        }else{

            NSLog(@"两个日期不相等");

        }

 

//练习 控制台输入一个时间间隔 如果据此时差值在60 就提示刚刚”  如果在60秒之外小于3600 就提示“XX 分钟前,如果大于 3600 就输出 “XX 小时前

        NSTimeInterval time = 0;

        printf("请输入时间");

        scanf("%lf",&time);

        if (time < 60) {

            NSLog(@"刚刚");

        }

        if(time > 60 && time < 3600){

            NSLog(@"%.f分钟前",time/60);

        }

        if (time > 3600) {

            NSLog(@"%.f小时前",time/60/60);

        }

时区的转化那点事       http://my.oschina.net/yongbin45/blog/151376

 下午所学:

分类:1.作用是为不知道源代码的类提供方法(扩展功能)

 2. 分类并不是一个类,只是为我们原有的类扩展功能,就是添加方法,而且只能添加方法,不能添加实例变量

cmd + ctrl +方向键 对(.h 与 .m文件的切换)

对于分类的使用,也需要在主文件里导入

 Extension 与 category 的区别:前者是一个类的私有方法,后者是为不知道源代码的类的方法的扩展

 1.制定协议内容

         2.设置代理人对象

         3.制定代理人

         4.代理人对象服从协议

         5.代理人实现协议中的方法

         6.通知代理人执行

//笑笑糖语法只能给不可变数组用

//代理delegate

 1 //
 2 //  main.m
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Man.h"
11 #import "Woman.h"
12 #import "BabySister.h"
13 #import "Mother.h"
14 
15 //1.制定协议
16 //2。对象遵循协议
17 //3.设置代理人
18 //4.代理人实现协议中的方法
19 //5.通知代理人去执行
20 //6.代理人去执行协议
21 int main(int argc, const char * argv[]) {
22     @autoreleasepool {
23         // insert code here...
24         NSLog(@"故事开始, World!");
25         NSLog(@"在某一天,一个风尘仆仆的美男子,出现在一个漆黑的夜晚....");
26         NSLog(@"突然...");
27         NSLog(@"前面出现了一个美如天仙的如花姑娘。。。");
28         NSLog(@"很快,两人就这样认识了");
29         NSLog(@"很快,两人就步入了婚姻的殿堂");
30         Woman *honey = [[Woman alloc]initWithName:@"shuhao" age:@"22"];
31         Man *man = [[Man alloc]initWithName:@"pengpeng" age:@"24"];
32         NSLog(@"shuhao:你要是帮我做这几件事,我就嫁给你");
33         NSLog(@"pengpeng :I DO");
34         //设置代理人
35         [honey setDelegate:man];
36         //通知代理人执行
37         [honey comeon];
38         
39         
40         Mother * mother = [[Mother alloc]initWithName:@"mom" age:@"35"];
41         BabySister * sister = [[BabySister alloc]initWithName:@"xiao" age:@"23"];
42 
43        
44         
45     }
46     return 0;
47 }
View Code  main.m 文件
 1 //
 2 //  Man.h
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import "Person.h"
10 #import "Woman.h"
11 
12 //代表 Man 这个类服从 MarriageDeal 的协议
13 @interface Man : Person<MarriageDeal>//一个类服从某协议的写法
14 //履行协议
15 - (void)cook;
16 - (void)doHousework;
17 - (void)makeABaye;
18 @end
View Code man.h 文件
//
//  Man.m
//  Protocol(协议)
//

#import "Man.h"

@implementation Man
//履行协议
- (void)cook{
    NSLog(@"给老婆做饭吃");
}
- (void)doHousework{
    NSLog(@"给老婆做家务,不容易啊");
}
- (void)makeABaby{
    NSLog(@"好吧,做个玩具娃娃");
}
@end
View Code man.m 文件
 1 //
 2 //  Woman.h
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import "Person.h"
10 //婚前约定(
11 //做饭 做家务 生孩子
12 @protocol MarriageDeal <NSObject>
13 //@required 是必须实现的方法(只要其他的类遵循这个协议那就必须去做)
14 //@optional 是可以选择实现的方法
15 @required//强制必须做的
16 - (void)cook;
17 - (void)doHousework;
18 @optional//可选做的,可实现,也可以不实现
19 //协议的默认状态是@required
20 - (void)makeABaby;
21 @end
22 
23 
24 @interface Woman : Person
25 {
26     //代理
27     id<MarriageDeal>_delegate;//设置代理对象
28     //<协议名>  代表代理服从协议
29     //谁去执行谁就是代理
30     //< > 里面是协议
31     //id就是可以代之所有的对象
32     
33 }
34 //为代理写setter 方法
35 - (void)setDelegate:(id)delegate;
36 //通知代理人去是实现对应的方法
37 - (void)comeon;
38 @end
View Code  woman.h 文件
 1 //
 2 //  Woman.m
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import "Woman.h"
10 
11 @implementation Woman
12 //为代理写setter 方法
13 - (void)setDelegate:(id)delegate{
14     _delegate = delegate;
15 }
16 //通知代理人去是实现对应的方法
17 - (void)comeon{
18     if (_delegate) {
19         [_delegate cook];
20         [_delegate doHousework];
21         [_delegate makeABaby];
22     }
23 }
24 @end
View Code  woman.m 文件
 1 //
 2 //  Person.h
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Person : NSObject
12 {
13     NSString *_name;
14     NSString *_age;
15 }
16 //初始化方法
17 - (id)initWithName:(NSString *)name
18                age:(NSString *)age;
19 
20 @end
View Code  person.h文件
 1 //
 2 //  Person.m
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import "Person.h"
10 
11 @implementation Person
12 //初始化方法
13 - (id)initWithName:(NSString *)name
14                age:(NSString *)age{
15     self = [super init];
16     if (self) {
17         _name  = name;
18         _age = age;
19     }
20     return self;
21 }
22 @end
View Code  person.m文件

// 分类 category

 1 //
 2 //  main.m
 3 //  Category
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "NSString+Addition.h"
11 #import "NSDictionary+Addition.h"
12 
13 int main(int argc, const char * argv[]) {
14     @autoreleasepool {
15         // insert code here...
16         NSLog(@"Hello, World!");
17         //category 分类 类目 为不知道源代码的一些类(系统的类)来扩充功能
18         NSString * str = @"HHH";
19         [str sayHi];
20         //只能添加方法
21         //为 NSString 扩充方法 :获取字符串的首字母并大写
22         NSString * oneFirst = [str getFirstCharacter];
23         NSLog(@"%@",oneFirst);
24         NSString * dataSet = @"20150819";
25         NSString * new = [dataSet change];
26         NSLog(@"%@",new);
27         NSString * strr = @"123456";
28         [strr stringToValue];
29         NSInteger b = 23;
30         NSString * new2 = [dataSet transToString:b];
31         NSLog(@"%@",new2);
32         NSDictionary * dic = @{@"Y":@"Yob",@"N":@"niu",@"Z":@"Zhang"};
33         NSArray * keys = [dic allKeysOrdered];
34         NSLog(@"%@",keys);
35 
36     }
37     return 0;
38 }
View Code   main.m  
 1 //
 2 //  NSString+Addition.h
 3 //  Category
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 //分类:1.作用是为不知道源代码的类提供方法(扩展功能)
12 // 2. 分类并不是一个类,只是为我们原有的类扩展功能,就是添加方法,而且只能添加方法,不能添加实例变量
13 @interface NSString (Addition)
14 - (void)sayHi;
15 //获取字符串的首字母并以大写的形式
16 - (NSString *)getFirstCharacter;
17 //把对应的字符串日期转化为特定的形式@"20150819"--@"2015.08.19"
18 - (NSDate *)change;
19 //把基本整型转化为字符串类型
20 - (NSString *)transToString:(NSInteger)num;
21 //把字符串转化为基本的整型
22 - (NSInteger)stringToValue;
23 
24 @end
View Code   NSString+Addition.h  
 1 //
 2 //  NSString+Addition.m
 3 //  Category
 4 //
 5 //
 8 
 9 #import "NSString+Addition.h"
10 
11 @implementation NSString (Addition)
12 - (void)sayHi{
13     NSLog(@"我是一个分类的方法");
14 }
15 //获取字符串的首字母并以大写的形式
16 - (NSString *)getFirstCharacter{
17 //    capitalizedString 让首字母大写
18 //    return [[self substringToIndex:1]capitalizedString];
19 //    uppercaseString 是让字母全部大写
20     return [[self substringToIndex:1]uppercaseString];
21 }
22 //把对应的字符串日期转化为特定的形式@"20150819"--@"2015.08.19"
23 - (NSString *)change{
24     NSMutableString * newStr = [NSMutableString stringWithString:self];
25     if ([self length] < 8) {
26         NSLog(@"长度不够");
27     }else{
28         NSString *str = @"/";
29         [newStr insertString:str atIndex:4];
30         [newStr insertString:str atIndex:7];
31     }
32     return newStr;
33 }
34 //把基本整型转化为字符串类型
35 - (NSString *)transToString:(NSInteger)num{
36    return [NSString stringWithFormat:@"%lu",num ];
37 }
38 //把字符串转化为基本的整型
39 - (NSInteger)stringToValue{
40     return  [self integerValue];
41 }
42 @end
View Code   NSString+Addition.m    
 1 //
 2 //  NSDictionary+Addition.h
 3 //  Category
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 
12 @interface NSDictionary (Addition)
13 //获取所有的key值然后给key排序然后返回给我们
14 - (NSArray *)allKeysOrdered;
15 @end
View Code   NSDictionary+Addition.h
 1 //
 2 //  NSDictionary+Addition.m
 3 //  Category
 4 //
 5 //
 8 
 9 #import "NSDictionary+Addition.h"
10 
11 @implementation NSDictionary (Addition)
12 //获取所有的key值然后给key排序然后返回给我们
13 - (NSArray *)allKeysOrdered{
14     //
15 //    NSArray * allkeys = [self allKeys];
16 //    NSArray * sort = [allkeys sortedArrayUsingSelector:@selector( compare:)];
17 //    return sort;
18     //
19 //    return [[self allKeys]sortedArrayUsingSelector:@selector(compare:)];
20     //
21     return [[self allKeys]sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
22         return [obj1 compare:obj2];
23     }];
24 }
25 @end
View Code   NSDictionary+Addition.m 

 //延展(Extension)

 1 //
 2 //  main.m
 3 //  08-19(延展)
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 
12 int main(int argc, const char * argv[]) {
13     @autoreleasepool {
14         // insert code here...
15         NSLog(@"Hello, World!");
16         Person * per = [[Person alloc]init];
17         //下面的两个方法不可见的
18         //[per saygoodbye]; 在这里不能被调用,只能在类的内部使用
19         //[per introduce];
20         [per test];
21 
22     }
23     return 0;
24 }
View Code   main.m
 1 //
 2 //  Person.h
 3 //  08-19(延展)
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 //.h文件声明实例变量和方法 用于为外界提供接口
12 //.m文件具体实现方法
13 @interface Person : NSObject
14 - (void)test;
15 @end
View Code   person.h
 1 //
 2 //  Person.m
 3 //  08-19(延展)
 4 //
 5 //
 8 
 9 #import "Person.h"
10 
11 //在 Person.m 文件里添加一个 延展
12 @interface Person ()
13 {
14     //私有的实例变量,仅仅是内部访问的
15     NSString * _age;
16     CGFloat _height;
17 }
18 //下面的方法声明注释掉,下面一样可以实现,所以对于私有的方法可以直接在内部实现
19 //- (void)saygoodbye;
20 //- (void)introduce;
21 @end
22 //延展 Extension 主要是在 .m文件里面 为一个类添加私有的方法和实例变量,这些实例变量和方法就只能供应自己使用,外界不能使用
23 @implementation Person
24 - (void)test{
25     _age = @"18";
26     NSLog(@"测试。。");
27     [self saygoodbye];
28     [self introduce];
29 }
30 - (void)saygoodbye{
31     NSLog(@"goodbye");
32 }
33 - (void)introduce{
34     NSLog(@"我年龄是 %@ 岁",_age);
35 }
36 @end
View Code    person.m

 

.h文件声明实例变量和方法 用于为外界提供接口

.m文件具体实现方法

Category 1.分类是为一些没有源代码的类里面添加一些,源代码里没有实现的方法   2.注意他的文件的创建  3.分类用于我们向该类添加方法,而且只能是添加方法,不能再往里面添加实例变量。添加一个分类后的文件是

@implementation NSString (Addition)//注意这里的写法(这是系统自动生成的)  注意那个分类的创建步骤,记住

@end

 1.延展 是管理类的私有方法,把代码写到原始的 .m文件里面

延展吧一个方法用到的实例变量(这里是当前类就是私有的了)的声明,与实现都写在一个类(当前类里面),也就是@interface@end 与@implementation @end写在一起。延展 Extension 主要是在 .m文件里面 为一个类添加私有的方法和实例变量,这些实例变量和方法就只能供应自己使用,外界不能使用

注意:为一个类添加一些私有变量的写法 如Person类里面添加两个私有变量

#import "Person.h"

@interface Person ()//这里要注意与普通定义的区别

{

    NSString * _group;//家族背景

    NSString *_sattion;//社会地位

}

@end

@implementation Person

@end

注意:私有变量里面定义的方法可以吧方法的声明省去,直接在@implementation @end里面实现

protocol 1.协议是iOS开发中常⽤的技术。 

协议是⼀套标准(⼀堆⽅法的声明),只有.h⽂件。就像⼀张任
务清单(或便利贴),上⾯写了⼀堆需要处理的事。清单交给谁,
谁就要去完成清单上规定的任务。
接受协议的对象实现协议中定义的⽅法。即:清单交给谁,谁就
要去完成清单上规定的任务。

协议中方法的类型有 @optional 代理者可做可不做的         @required  代理者必须做的

协议protocol的定义的格式要注意,他是在@interface @end的@interface的上面写以 @protocol  方法清单 @end 书写

 

//获取字符串的首字母并以大写的形式

- (NSString *)getFirstCharacter{

//    capitalizedString 让首字母大写

//    return [[self substringToIndex:1]capitalizedString];

//    uppercaseString 是让字母全部大写

    return [[self substringToIndex:1]uppercaseString];

}

    NSString * str = @"123";     [str integerValue]; 把字数字符串转化为整型数字

 

 

posted @ 2015-08-19 08:30  ywda  阅读(355)  评论(0编辑  收藏  举报