黑马程序员——OC笔记之Foundation框架下

------- android培训java培训iOS培训.Net培训、期待与您交流! ----------

上篇描述了Foundation框架中NSString、NSMutableString、NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSRange的一些用法。

接下来要介绍是Foundation框架中NSFileManager、NDRect、NSPoint、NSSize、NSNumber、NSValue、NSDate的一些用法。

文件管理(NSFileManager)

顾名思义,NSFileManager是用来管理文件系统的。它可以用来进行常见的文件/文件夹操作(拷贝、剪切、创建等)。

NSFileManager使用了单例模式singleton

使用defaultManager方法可以获得那个单例对象[NSFileManager defaultManager] 

1、NSFileManager的基本用法

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSString *filePath=@"/Users/Linhan/Desktop";
        NSString *filePath1=@"/Users/Linhan/Desktop/array.plist";
        //NSFileManager 用于判断
        //创建一个单例对象:在程序运行期间,只有一个对象
        NSFileManager *fm=[NSFileManager defaultManager];
        
        //1)判断文件是否存在/Users/Linhan/Desktop/array.plist
        if([fm fileExistsAtPath:filePath]){
        
            NSLog(@"文件存在");
            BOOL isDir;
            //2)判断是否是一个目录
            [fm fileExistsAtPath:filePath isDirectory:&isDir];
            if (isDir) {
                NSLog(@"这是一个目录");
            }
        }
        
        //3)判断文件是否可读
        BOOL isYes=[fm isReadableFileAtPath:filePath1];
        NSLog(@"%d",isYes);
        //4)是否可写
        isYes=[fm isWritableFileAtPath:filePath1];
        NSLog(@"%d",isYes);
        //5)是否可删除
        isYes=[fm isDeletableFileAtPath:filePath1];
        NSLog(@"%d",isYes);

    }
    return 0;
}

2、NSFileManager的用法深入(一)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        //创建文件对象
        NSFileManager *fm=[NSFileManager defaultManager];
        NSString *filePath=@"/Users/Linhan/Desktop/array.plist";
        NSString *filePath1=@"/Users/Linhan/Desktop/12";
        
        //1)获取文件的信息(属性)
        NSDictionary *dict= [fm attributesOfItemAtPath:filePath error:nil];
        NSLog(@"%@",dict);
        
        //2)获取指定目录下的文件及子目录
        //使用递归迭代调用,获取当前目录及子目录下的所有的文件及文件夹
        NSArray *subPaths=[fm subpathsAtPath:filePath1];
        //subpathsOfDirectoryAtPath  不是使用递归方法调用
        subPaths=[fm subpathsOfDirectoryAtPath:filePath1 error:nil];
        NSLog(@"%@",subPaths);
        
        //3)获取指定目录下的子目录(不再获取后代路径)
        subPaths=[fm contentsOfDirectoryAtPath:filePath1 error:nil];
        NSLog(@"%@",subPaths);
    }
    return 0;
}

3、NSFileManager的用法深入(二)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        //创建文件管理对象
        NSFileManager *fm=[NSFileManager defaultManager];
        //如何创建目录
        NSString *path=@"/Users/Linhan/Desktop/12/13";
        NSString *path1=@"/Users/Linhan/Desktop/12/13/a/b";
//        [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
//        [fm createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
        
        //如何创建文件
        NSString *str=@"每当我错过一个女孩,我就往山上默默地放一块砖,后来就有了长城。";
        //创建NSData ,是一个处理二进制数据的类
        NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
        NSString *path2=@"/Users/Linhan/Desktop/12/13/a/love.txt";
        
//        [fm createFileAtPath:path2 contents:data attributes:nil];
        NSString *targetPath=@"/Users/Linhan/Desktop/12/13/a/b/love.txt";
        //如何复制文件
//        [fm copyItemAtPath:path2 toPath:targetPath error:nil];
        
        NSString *targetPath1=@"/Users/Linhan/Desktop/12/13/love.txt";
        //如何移动文件
        [fm moveItemAtPath:targetPath toPath:targetPath1 error:nil];
        //如何删除文件
        [fm removeItemAtPath:targetPath error:nil];
    }
    return 0;
}

常见的结构体(NDRect、NSPoint、NSSize)

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        //苹果官方推荐使用以CG开头
        //CGPoint 和 NSPoint(CGPoint代表的是二维平面中的一个点,可以使用CGPointMake和NSMakePoint函数创建CGPoint)
        CGPoint c;
        c.x=1;
        c.y=2;
        
        CGPoint c1={12,22};
        c1= CGPointMake(12, 22);
        
        NSPoint np1={1,3};
        np1= NSMakePoint(1, 3);
        //CGSize 和 NSSize(CGSize代表的是二维平面中的某个物体的尺寸(宽度和高度),可以使用CGSizeMake和NSMakeSize函数创建CGSize)
        CGSize c2={30,60};
        c2= CGSizeMake(20, 100);
        
        NSSize ns2={40,50};
        ns2= NSMakeSize(45, 59);
        //CGRect 和 NSRect(CGRect代表的是二维平面中的某个物体的位置和尺寸,可以使用CGRectMake和NSMakeRect函数创建CGRect)
        CGRect c3={{0,0},{40,66}};
        c3= CGRectMake(0, 0, 45, 66);
        
        NSRect nr1={{0,0},{50,100}};
        nr1= NSMakeRect(0, 0, 56, 44);
    }
    return 0;
}

处理基本数据(NSNumber)

NSArray/NSDictionary中只能存放OC对象,不能存放int/float/double等基本数据类。如果真想把基本数据(比如int)放进数组或字典中, 需要先将基本数据类型包装成OC对象
NSNumber可以将基本数据类型包装成对象,这样就可以间接将基本数据类型存进NSArray/NSDictionary中
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        //NSNumber 是OC中处理数值类型的一个类
        //如何使用:把 基本数据类型 包装成一个对象
        //好处:可以把基本数据类型的数据,保存到 数组 或 字典 中
        int a=3;
        float b=5.23f;
        double c=8.22;
        BOOL d=YES;
        NSNumber *intObj=[NSNumber numberWithInt:a];
        NSNumber *fObj=[NSNumber numberWithFloat:b];
        NSNumber *dObj=[NSNumber numberWithDouble:c];
        NSNumber *BObj=[NSNumber numberWithBool:d];
        
        NSMutableArray *arr=[NSMutableArray arrayWithObjects:intObj,fObj,dObj,BObj, nil];
        //将对象添加到数组中
        [arr addObject:fObj];
        [arr addObject:@(b)];
        [arr addObject:@5.23f];
        NSLog(@"%@",arr);

        //取出数据进行运算
        //1)取出数组元素
        //2)把数组元素转换为基本数据类型的数据
        NSNumber *n1=arr[0];
        int b1=[n1 intValue];
        NSNumber *n2=arr[1];
        float b2=[n2 floatValue];
        int a1=b1+b2;
        
        float a2=[arr[0]intValue]+[arr[1]floatValue];
        NSLog(@"%d,%.2f",a1,a2);

    }
    return 0;
}

包装(NSValue)

NSValue可以用来包装对象指针,CGRect结构体等 
NSValue对象可以保存任意类型的数据,比如int,float,char,也可以是指pointers。NSValue类的目标就是允许以上数据类型的数据结构能够被添加到集合里,如NSArray或者NSSet的实例。
注意:NSValue对象一直是不可枚举的。
NSMutableArray *mArr=[NSMutableArray array];
NSPoint np=NSMakePoint(10, 10);
NSRect nr=NSMakeRect(0, 0, 45, 66);

//将指针、结构体转换成NSValue对象
NSValue *val=[NSValue valueWithPoint:np];

//将NSValue对象添加到数组中
[mArr addObject:val];
[mArr addObject:[NSValue valueWithRect:nr]];
NSLog(@"%@",mArr);

//从NSValue对象中,取得对应类型的值
NSValue *npVal=[mArr lastObject];
NSRect nr3=[npVal rectValue];
NSPoint np3=[mArr[0]pointValue];
//将NSRect类型的结构体转换成字符串输出
NSLog(@"%@\n%@",NSStringFromPoint(np3),NSStringFromRect(nr3));
//定义结构体的同时起一个myDate的别名
typedef struct{

    int year;
    int month;
    int day;
} myDate;
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //将结构体存放到数组中
        //保存一个日期:2012-12-21
        myDate md={2012,12,21};
        //@encode(myDate)的作用,是把myDate类型生成一个常量字符串的描述
        NSValue *val2=[NSValue valueWithBytes:&md objCType:@encode(myDate)];
        NSArray *arr=[NSArray arrayWithObjects:val2, nil];
        NSLog(@"%@",arr);
        
        //从对象中取出结构体变量的值
        //传入一个结构体变量的地址
        myDate tmd;
        [val2 getValue:&tmd];
        NSLog(@"%d,%d,%d",tmd.year,tmd.month,tmd.day);
        test();
        
    }
    return 0;
}

日期(NSDate)

NSDate可以用来表示时间, 可以进行一些常见的日期/时间处理
一个NSDate对象就代表一个时间
以下是NSDate的用法:

1、输出格林威治时间/系统时间(当前时区)

//获取0时区时间
NSDate *date=[NSDate date];
//输出格林威治时间,0时区
NSLog(@"%@",date);

//转换成系统时间,北京时间
//设置时区
NSTimeZone *zone=[NSTimeZone systemTimeZone];

//设置时间间隔
NSInteger interval=[zone secondsFromGMTForDate:date];

//添加时间间隔,重新生成时间
NSDate *localeDate=[date dateByAddingTimeInterval:interval];
NSLog(@"%@",localeDate);
}

2、格式化输出系统时间

NSDate *date=[NSDate date];
NSLog(@"%@",date);//格式化输出时间日期

//创建一个格式化对象
NSDateFormatter *formatter=[NSDateFormatter new];

//创建日期的显示格式
//yyyy:表示4位的年份  yy:表示2位的年份
//HH:24小时制的小时  hh:12小时制的小时
formatter.dateFormat=@"yyyy年MM月dd日 HH:mm:ss";

//格式化日期(自动转换成系统时间)
NSString *myDate=[formatter stringFromDate:date];
NSLog(@"%@",myDate);

3、计算时间(更改时间输出)

//计算日期
//1)明天的此刻(0时区)
//设置时间间隔(秒),将其值改为-,则为昨天时间(0时刻)
NSTimeInterval secondsPerDay=24*60*60;
NSDate *theDate=[NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"theDate=%@",theDate);

//2)明天的此刻(系统时间)
NSDateFormatter *formatter=[NSDateFormatter new];

//创建日期的显示格式
formatter.dateFormat=@"yyyy年MM月dd日 HH:mm:ss";

//格式化日期(自动转换成系统时间)
NSString *myDate=[formatter stringFromDate:theDate];
NSLog(@"theDate=%@",myDate);

4、获取日期时间的元素

//获取部分时间日期对象(系统时间)
//NSCalendar 日期类,可以帮我们快速获取年月日时分秒信息
//创建日期
NSDate *date=[NSDate date];

//创建日期对象
NSCalendar *calendar=[NSCalendar currentCalendar];

//获得时间元素
//NSDateComponents *cms=[calendar components:<#(NSCalendarUnit)#> fromDate:<#(NSDate *)#>]
NSDateComponents *cms=[calendar components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:date];
NSLog(@"%ld:%ld:%ld",cms.hour,cms.minute,cms.second);

5、比较日期的时间差

//比较时间的差距
NSString *time1=@"2015-08-03 13:43:30";
NSString *time2=@"2015-06-27 09:12:33";

//定义时间格式
NSDateFormatter *fmt=[NSDateFormatter new];
fmt.dateFormat=@"yyyy-MM-dd HH-mm-ss";

//格式化创建日期
NSDate *date1=[fmt dateFromString:time1];
NSDate *date2=[fmt dateFromString:time2];

//创建一个日历对象
NSCalendar *calendar=[NSCalendar currentCalendar];

//比较时间的差距
int unit=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *cmps=[calendar components:unit fromDate:date2 toDate:date1 options:0];

NSLog(@"%@",cmps);
NSLog(@"相差%ld年%ld月%ld日%ld时%ld分%ld秒",cmps.year,cmps.month,cmps.day,cmps.hour,cmps.minute,cmps.second);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值