您的位置:首页 > 职场人生

黑马程序员—Foundation框架之NSFileManager、NSNumber与NSDate

2015-10-10 20:54 393 查看
一、NSFileManager

1、NSFileManager是用来管理文件系统的

2、它可以用来进行常见的文件\文件夹操作(拷贝、剪切、创建等)

3、NSFileManager使用了单例模式singleton

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

5、NSFileManager的常见判断

1)- (BOOL)fileExistsAtPath:(NSString *)path:path这个文件或文件夹是否存在

2)- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory:path这个文件或文件夹是否存在, isDirectory代表是否为文件夹

3)- (BOOL)isReadableFileAtPath:(NSString *)path:path这个文件或文件夹是否可读

4)- (BOOL)isWritableFileAtPath:(NSString *)path:path这个文件或文件夹是否可写

5)- (BOOL)isDeletableFileAtPath:(NSString *)path:path这个文件或文件夹是否可删除

#import <Foundation/Foundation.h>

int main()
{
NSString *filePath = @"/Users/zhaoxiaohu/Desktop/arr.plist";

NSString *filePath2 = @"/";

// 1) 判断文件是否存在
//创建文件管理对象
//调用defaultManager 创建一个文件管理的单例对象
//单例对象:在程序运行期间,只有一个对象存在
NSFileManager *fm = [NSFileManager defaultManager];
// YES 存在   NO 不存在
BOOL isYES = [fm fileExistsAtPath:filePath];
NSLog(@"-->%d",isYES);

if(isYES){
BOOL isDir;

// 2) 判断是否是一个目录
[fm fileExistsAtPath:filePath isDirectory:&isDir];

if (isDir) {
NSLog(@"这是一个目录");
}else{

NSLog(@"这不是一个目录");
}

}

// 3) 判断文件是否可读

isYES = [fm isReadableFileAtPath:filePath];

// 4) 是否可写
isYES = [fm isWritableFileAtPath:filePath2];

// 5) 是否可删除
isYES = [fm isDeletableFileAtPath:filePath2];

NSLog(@"-->%d",isYES);
return 0;
}


6、NSFileManager的文件访问

1)- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error:获得path这个文件\文件夹的属性

2)- (NSArray *)subpathsAtPath:(NSString *)path

3)- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error:获得path的所有子路径(后代路径),上面两个方法功能一样。

4)- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error:获得path的当前子路径(path下的所有直接子内容,path必须是一个目录)

5)- (NSData *)contentsAtPath:(NSString *)path:获得文件内容

#import <Foundation/Foundation.h>

int main()
{
//创建文件对象
NSFileManager *fm = [NSFileManager defaultManager];
NSString *filePath = @"/Users/zhaoxiaohu/Desktop/arr.plist";

NSString *dirPath = @"/Users/zhaoxiaohu/Desktop/a";

//1)如何获取文件的信息(属性)
NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dict);
NSLog(@"%@,%@",[dict objectForKey:@"NSFileOwnerAccountName"],dict[@"NSFileOwnerAccountName"]);

//2)获取指定目录下文件及子目录
//使用递归的方式 获取当前目录及子目录下的所有的文件及文件夹
NSArray *subPaths = [fm subpathsAtPath:dirPath];

//subpathsOfDirectoryAtPath 不是使用递归的方式获取的
subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil];
NSLog(@"subPaths = %@",subPaths);

//3)获取指定目录下的文件及目录信息(不在获取后代路径)
subPaths = [fm contentsOfDirectoryAtPath:dirPath error:nil];
NSLog(@"subPaths = %@",subPaths);
return 0;
}
7、NSFileManager的文件操作

1)- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error:拷贝

2)- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error:移动(剪切)

3)- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error:删除

4)- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error:只能创建文件夹(createIntermediates为YES代表自动创建中间的文件夹)

5)- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr:创建文件(NSData是用来存储二进制字节数据的)

二、NSNumber

1、把基本数字类型包装成OC对象

1)NSArray\NSDictionary中只能存放OC对象, 不能存放int\float\double等基本数据类

2)如果真想把基本数据(比如int)放进数组或字典中, 需要先将基本数据类型包装成OC对象

2、NSNumber的常用方法

1)- (char)charValue;

2)- (int)intValue;

3)- (long)longValue;

4)- (double)doubleValue;

5)- (BOOL)boolValue;

6)- (NSString *)stringValue;

7)- (NSComparisonResult)compare:(NSNumber *)otherNumber;

8)- (BOOL)isEqualToNumber:(NSNumber *)number;

#import <Foundation/Foundation.h>

int main()
{
int a = 10;
int x = 100;
float f1 = 3.23f;
double d1 = 2.34;

// numberWithInt   把int   转换为 NSNumber
NSNumber *intObj = [NSNumber numberWithInt:a];
NSMutableArray *array = [NSMutableArray arrayWithObjects:intObj, nil];

//  float ---> NSNumber
NSNumber *floatObj = [NSNumber numberWithFloat:f1];
[array addObject:floatObj];

// double ---> NSNumber
NSNumber *dObj = [NSNumber numberWithDouble:d1];
//把对象添加到数组中
[array addObject:dObj];
//@(变量名)  ---> 把 x 转换为NSNumber对象
[array addObject:@(x)];
//@数值,把数值包装成对象
[array addObject:@18];

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

//1)取出数组的元素
//2)把数组元素转换为基本数据类型的数据
NSNumber *n1 = array[0];
int a1 = [n1 intValue];  //获取对象的整形值

NSNumber *n2 = array[1];
float f2 = [n2 floatValue];

a1 = [array[0] intValue]+[array[1] floatValue];
NSLog(@"%d",a1);
return 0;
}
注意:

1)NSNumber是NSValue的子类, 但NSNumber只能包装数字类型

2)NSValue可以包装任意值

3)可以用NSValue将结构体包装后, 加入NSArray\NSDictionary中

三、NSDate

1、NSDate可以用来表示时间, 可以进行一些常见的日期\时间处理

2、一个NSDate对象就代表一个时间

3、[NSDate date]返回的就是当前时间

#import <Foundation/Foundation.h>

int main()
{

NSDate *d1 = [NSDate date];

NSLog(@"%@",d1);
//<span style="font-family: Arial, Helvetica, sans-serif;">2015-10-10 12:39:46 +0000</span>

//格式化日期
//NSDateFormatter 日期格式化
NSDateFormatter *formatter = [NSDateFormatter new];
//设置日期的显示格式的

formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//格式化日期
NSString *dateStr = [formatter stringFromDate:d1];
NSLog(@"%@",dateStr);
return 0;
}
注意:

1)获取的是0时区时间,要想得到北京时间还要当前时间+8小时

2)设置日期显示格式:yyyy 表示四位的年份, MM 表示2位的月份,dd 表示2位的天数,HH 表示24小时制得小数 hh - 12小时值, mm 表示2位的分钟数, ss 表示2位的秒数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: