OC第七天

来源:互联网 发布:ubuntu 安装chrome 编辑:程序博客网 时间:2024/05/21 22:24

NSString+SayHello.h

#import <Foundation/Foundation.h>//文件名中: +加号前面是被扩展的类,后面是类名//括号中是分类的名字,NSString是被扩展的类@interface NSString (SayHello){//    NSString *_s;分类中不能声明实例变量}//分类中只能声明方法 不能声明实例变量-(void) hello;@end

NSString+SayHello.m

#import "NSString+SayHello.h"@implementation NSString (SayHello)-(void)hello{    NSLog(@"hello");}@end

SubString.h

#import <Foundation/Foundation.h>@interface SubString : NSString@end

SubString.m

#import "SubString.h"//延展的名字//延展只有声明部分,没有实现部分;声明延展的格式与声明分类的格式一样@interface SubString (){    NSString *_age;    //延展可以给类增加实例变量,但是增加的实例变量只能在本类的.m文件中使用,在其他地方访问不到}//在延展中声明的方法,也只能在.m中使用-(void)print;@end@implementation SubString-(void)print{    _age =@"30";}@end

NSDate+DateWithString.h

#import <Foundation/Foundation.h>@interface NSDate (DateWithString)//声明一个构造便利器+(NSDate *)dateWithDateString:(NSString *)dateString;@end

NSDate+DateWithString.m

#import "NSDate+DateWithString.h"@implementation NSDate (DateWithString)//实现构造便利器+(NSDate *)dateWithDateString:(NSString *)dateString{    NSDateFormatter *dateFormat = [[NSDateFormatteralloc]init];    [dateFormat setDateFormat:@"yyyyMMddHHmmss"];    NSDate *date = [dateFormatdateFromString:dateString];    return date;};@end

MarryProtocol.h

#import <Foundation/Foundation.h>//协议只有.h文件没有.m;protocol只有声明没有实现@protocol MarryProtocol <NSObject>//协议里面的方法分为两种:一种为必须实现(@required),另一种为可实现、可不实现(@optional)//必须实现的方法:表示一个类如果遵守了协议,那么在这个类的.m文件中就必须实现这个方法;若没有关键字对方法说明,默认必须实现@required-(void)wash;//可实现可不实现的方法:表示一个类如果遵守了协议,那么方法在.m中不一定实现@optional-(void)noSmoking;@end

Boy.h

import

import “MarryProtocol.h”

//遵守协议;格式是在父类后面用尖括号将协议名称括起来;遵守完协议的类,就可以实现协议里面声明的方法;

@interface Boy : NSObject

@end

Boy.m

#import "Boy.h"@implementation Boy//wash 这个方法是在协议里面进行声明的,Boy类遵守协议,所以实现的wash方法就是协议里面声明的;-(void)wash{    NSLog(@"大人洗碗了!");}//print 么有在协议中进行声明,是Boy这个类的私有方法,是不对外公开的,作用范围在.m文件中,在.m外不能使用-(void)print {}@end

Girl.h

#import <Foundation/Foundation.h>#import "MarryProtocol.h"@interface Girl : NSObject{//    代理的类型是遵守协议。对象类型。    id<MarryProtocol> _delegate;}//声明setter设置器-(void)setDelegate:(id<MarryProtocol>)delegate;//声明getter获取器-(id<MarryProtocol>)delegate;//声明一个不洗碗方法-(void)noWash;@end

Girl.m

#import "Girl.h"@implementation Girl//实现setter设置器-(void)setDelegate:(id<MarryProtocol>)delegate{    _delegate = delegate;}//实现getter获取器-(id<MarryProtocol>)delegate{    return_delegate;}//实现不洗碗方法-(void)noWash{//    碗必须得洗,让代理去执行    [_delegatewash];}@end

main.m

#import <Foundation/Foundation.h>#import "NSString+SayHello.h"#import "SubString.h"#import "NSDate+DateWithString.h"#import "Boy.h"#import "Girl.h"int main(int argc,const char * argv[]) {    /*//    获取的是系统的当前时间,系统当前时间是0时区    NSDate *nowDate = [NSDate date];    NSLog(@"nowDate = %@",nowDate);//    获取东八区的时间    NSDate *bjDate = [NSDate dateWithTimeIntervalSinceNow:8*60*60];    NSLog(@"bjDate = %@",bjDate);//    获取昨天的时间    NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60+8*60*60];    NSLog(@"yesterdayDate = %@",yesterdayDate);//    获取明天时间    NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:+24*60*60+8*60*60];    NSLog(@"tomorrowDate = %@",tomorrowDate);//    计算两个时间对象的时间差,以秒为单位    NSTimeInterval timeInterval = [tomorrowDate timeIntervalSinceDate:yesterdayDate];    NSLog(@"timeInterval = %lu",timeInterval);//    获取时间戳    NSTimeInterval time = [nowDate timeIntervalSince1970];    NSLog(@"time = %lu",time);//    练习1     //计算当前时间和⼀一个固定时间的差值,如果差值在60秒内,输出“刚刚”,如果在60秒外3600秒内,输出“xx分钟前”,如果3600秒外,3600*24秒内,输出“xx小时前”。    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:7*60];    NSTimeInterval subTime = [date timeIntervalSinceDate:nowDate];    if (subTime < 60) {        NSLog(@"刚刚");    } else if (subTime >= 60 && subTime < 3600){        NSLog(@"%d分钟前",(int)subTime / 60);    } else {        NSLog(@"%d小时前",(int)subTime / 3600);    }    NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];    [forMatter setDateFormat:@"yyyy年MM月dd日HH:mm:ss"];    NSString *dateStr = [forMatter stringFromDate:nowDate];    NSLog(@"dateStr = %@",dateStr);    NSString * ddateStr = @"2008-08-08 20:08:08";    [forMatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    NSDate *date1 = [forMatter dateFromString:ddateStr];    NSLog(@"date1 = %@",date1);    */    NSString *s = [[NSStringalloc] init];//    分类中的方法,字符串对象都能使用;相当于给字符串类增加了方法(API)    [s hello];    SubString *subStr = [[SubStringalloc]init];//    分类的所有方法被子类全部继承    [subStr hello];//    练习3    NSDate *dada = [NSDatedateWithDateString:@"20140402142850"];    NSLog(@"dada = %@",dada);//    创建boy和girl对象    Boy *boy = [[Boyalloc]init];    Girl *girl = [[Girlalloc]init];//    [boy wash];//    girl对象完成代理设置,即设置boy为她的代理    [girl setDelegate:boy];    [girl noWash];    return0;}
0 0
原创粉丝点击