码迷,mamicode.com
首页 > 其他好文 > 详细

obj-c9[[NSDate,{Category分类,Extension,管理'私有”方法,Protocol (协议)}]

时间:2015-01-22 07:07:04      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:import   邮箱   

#import <Foundation/Foundation.h>
//#import "Person.h"
#import "NSString+SayHiMessage.h"
#import
"NSMutableArray+ChangeArray.h"

#import "NSString+ExchangeChineseToEnglish.h"

#import "NSString+EmailValidation.h"//邮箱验证
#import "NSArray+PartitionArray.h"
int main(int argc, const char * argv[]) {
   
@autoreleasepool {
                           
//类的扩展

        //1.NSDate和NSDateForMatter

        //获取当前时间
       
//但是打印出来的就是零时区的时间
       
//NSLog(@“%@”,nowDate);无论你是哪个时区的时间,打印时总是打印对应的0时区时间

//        NSDate * date = [NSDate date];
//        NSLog(@"%@",date);
//        //日期格式类,用于日期和字符串之间的相互转换
//        NSDateFormatter * dateformatter = [[NSDateFormatter alloc] init];
//        //设置日期的转换格式
//        //E周几 EEEE星期几
//        //HH是24小时制, hh是12小时制  a是上下午
//        dateformatter.dateFormat = @"yyyy-MM-dd EEEE hh:mm:ss a";
//        //使用dateFormat把日期转换成字符串
//        NSString * dateString = [dateformatter stringFromDate:date];
//        NSLog(@"%@",dateString);.
       
       
//获取当前时间多少秒之后的一个时间
//        NSDate  * date = [NSDate dateWithTimeIntervalSinceNow:3600];
//        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
//        [formatter setDateFormat:@"yyyy-MM-dd EEEE hh:mm:ss"];
//        NSString * str =  [formatter stringFromDate:date];
//        NSLog(@"%@",str);
   
//-------------------------------------------------
//        //服务器端给我们返回的时间戳(字符串类)
//        NSString * timeString = @"20150101093245";
//        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
//        //转换格式要一致
//        [formatter setDateFormat:@"yyyyMMddhhmmss"];
//        //借助NSDateFormatter 把字符串转换日期
//        NSDate * getDate = [formatter dateFromString:timeString];
//        NSLog(@"%@",getDate);
//       
//        //获取当前时间
//        NSDate * currentDate = [NSDate date];
//        //获取两个时间差
//        NSTimeInterval seconds = [currentDate timeIntervalSinceDate:getDate ];
//        NSLog(@"%.1f秒前",seconds);
//        if (seconds >= 0 && seconds <= 300 ) {
//            NSLog(@"刚刚..");
//        }else if (seconds > 300 && seconds <= 3600){
//            NSLog(@"%d分钟之前..",(int)seconds / 60);
//        }else if (seconds > 3600 && seconds <=86400){
//            NSLog(@"%d小时前..",(int)seconds / 3600);
//        }else if (seconds > 86400 && seconds <=259200){
//            NSLog(@"%d天前..",(int) seconds / 86400);
//        }else if(seconds > 259200){
//            NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
//            [formatter setDateFormat:@"yyyy-MM-dd"];
//            NSString * str =  [formatter stringFromDate:getDate];
//            NSLog(@"%@",str);
//        }
//-------------------------------------------------
       
       
//从1970年到当前时间流逝的秒数
//        NSDate * currentDate = [NSDate date];
//        NSTimeInterval seconds = [currentDate timeIntervalSince1970];//currentDate.timeIntervalSince1970;
//        NSLog(@"%.1f",seconds);
       
       
//从一个1970年流逝的秒数得到一个时间
//        NSTimeInterval seconds = 1251253251;
//        NSDate * getDate = [NSDate dateWithTimeIntervalSince1970:seconds];
//        NSLog(@"%@",getDate);
       
       
       
//2.类目(分类)  Category主要作用是为没有源代码的类添加方法()
//        NSString * str = @"蓝鸥";
//        [str sayHi];
       
//        NSString * str = @"河南";

//        [str sayHello];

 //数组逆序输出
- (
void)sortedArray{
    
for (int i = 0; i < [self count] / 2; i++) {
         [
self exchangeObjectAtIndex:i withObjectAtIndex:([self count]  - 1 - i)];
    }

}    // .

//        NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1", @"2",@"3",@"4", @"5", nil ];
//        [array sortedArray];
//        NSLog(@"%@",array);

       - (NSString *)transformMandarinToLatin
{
   
/*复制出一个可变的对象*/
   
NSMutableString *preString = [self mutableCopy];
   
/*转换成成带音 调的拼音*/
   
CFStringTransform((CFMutableStringRef)preString, NULL, kCFStringTransformMandarinLatin, NO);
   
/*去掉音调*/
   
CFStringTransform((CFMutableStringRef)preString, NULL, kCFStringTransformStripDiacritics, NO);
   
   
/*多音字处理*/
   
if ([[(NSString *)self substringToIndex:1] compare:@"长"] == NSOrderedSame)
    {
        [preString
replaceCharactersInRange:NSMakeRange(0, 5) withString:@"chang"];
    }
   
if ([[(NSString *)self substringToIndex:1] compare:@"沈"] == NSOrderedSame)
    {
        [preString
replaceCharactersInRange:NSMakeRange(0, 4) withString:@"shen"];
    }
   
if ([[(NSString *)self substringToIndex:1] compare:@"厦"] == NSOrderedSame)
    {
        [preString
replaceCharactersInRange:NSMakeRange(0, 3) withString:@"xia"];
    }
   
if ([[(NSString *)self substringToIndex:1] compare:@"地"] == NSOrderedSame)
    {
        [preString
replaceCharactersInRange:NSMakeRange(0, 3) withString:@"di"];
    }
   
if ([[(NSString *)self substringToIndex:1] compare:@"重"] == NSOrderedSame)
    {
        [preString
replaceCharactersInRange:NSMakeRange(0, 5) withString:@"chong"];
    }
   
return preString;

}

        //姓名转换拼音
//        NSString * str = @"张三";
//        NSString * string = [str transformMandarinToLatin];
//        NSLog(@"%@",string);
       


        //邮箱验证

-(BOOL)isValidateEmail {
//利用正则表达式验证

    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTest evaluateWithObject:self];

}

//        NSString * str = @"zhangsan@qq.com";
//        BOOL result = [str isValidateEmail];
//        NSLog(@"%@",result ? @"YES" : @"NO");

       

- (NSArray *)partitionArray:(int)n{
   
NSMutableArray * array = [NSMutableArray arrayWithCapacity:1];
   
for (int i = 0; i < [self count]; i++) {
       
if (i % n == 0) {
           
NSMutableArray * smallArray = [NSMutableArray arrayWithCapacity:1];
            [array
addObject:smallArray];//添加小数组
        }
        [[array
lastObject] addObject:[self objectAtIndex:i]];//往小数组里面加元素
        }
   
return [NSArray arrayWithArray:array];

}

        //大数组分割小数组
//        NSArray * arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
//        NSArray * array = [arr partitionArray:(2)];
//        NSLog(@"%@",array);
       
       
//3.Extension 延展 作用:便于管理类的"私有"方法和私有实例变量
       
//oc中没有绝对的私有方法

        //oc"私有"方法就是指,在.h里面没有声明,只在.m有实现的方法


//4.协议就是一堆方法声明

//只有类可以接受协议接受协议使用<>,如果一个类接受一个协议,就必须实现协议里面的@required方法,一个类可以接受多个协议,多个协议之间,逗号,隔开.

//协议里面的方法,使用@required是必须实现,使用@optional是可选实现,不写默认是@required

main.m

        Boy * boy = [[Boy alloc] init];
       
       
Gril * gril1 = [[Gril alloc] init];
       
        gril1.
delegate = boy;
       

        [gril1 hungry:@"青椒鸡蛋面."];

@protocol TestProtocol <NSObject>

//methods//方法
@required//必须的实现方法
- (
void)test1;
- (
void)test2;
@optional//可选的实现方法

- (void)test3;

@end


@interface Person : NSObject <TestProtocol, OtherProtocol>/*接受多个协议*/

@end


@implementation Gril
- (
void)hungry:(NSString *)wantFood{
   
//"询问"代理,得到一个结果
   
if([_delegate respondsToSelector:@selector(doHouseWord)]){
       
   
BOOL result = [_delegate doHouseWord];
   
if (result) {
       
NSLog(@"完成了");
       
NSLog(@"开始做饭");
       
//NSObject的方法,判断一个对象是否实现了某个方法
       
if ([_delegate respondsToSelector:@selector(doCook:)]) {
           
//让代理做饭
            [
_delegate doCook:wantFood];
        }
    }
else{
       
NSLog(@"继续干..");
    }

    }

}

@end

#import <Foundation/Foundation.h>

@protocol MarriedProtocol <NSObject>

@required
- (
void)doCook:(NSString *)FoodName;
- (
BOOL)doHouseWord;
@optional
- (
void)dirveCar;

@end

@interface Gril : NSObject
//id<MarriedProtocol>/*基于类型的限定*/
//MarriedProtocol协议的类的对象,才能成为她的丈夫
@property (nonatomic,assign)id<MarriedProtocol> delegate;


- (
void)hungry:(NSString *)wantFood;

@end


本文出自 “IOS--培训” 博客,请务必保留此出处http://zys2007.blog.51cto.com/8207599/1606819

obj-c9[[NSDate,{Category分类,Extension,管理'私有”方法,Protocol (协议)}]

标签:import   邮箱   

原文地址:http://zys2007.blog.51cto.com/8207599/1606819

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!