NSDictionary
key:OC对象
value:不能是非OC对象
NSNumber
- 对数组进行一系列的操作
NSNumber *sum = [array valueForKeyPath:@"@sum.integerValue"];
NSNumber *max = [array valueForKeyPath:@"@max.integerValue"];
NSNumber *min = [array valueForKeyPath:@"@min.integerValue"];
NSNumber *avg = [array valueForKeyPath:@"@avg.integerValue"];
NSNumber *distinct = [array valueForKeyPath:@"@distinctUnionOfObjects.integerValue"];
NSLog(@"array = %@, sum = %@ ,max = %@ , min = %@, avg = %@, distinct = %@",array,sum,max,min,avg,distinct);
NSFileManager/NSFileHandle
//创建文件
NSString *path = @"/Users/frankfan/desktop/demoFile.txt";
[fileManager createFileAtPath:path contents:[@"hello world hellelellelelelelelelelele" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//创建文件夹
NSError *error;
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"errro:%@",error);
//判断文件是否存在
BOOL isFileExi = [fileManager fileExistsAtPath:path isDirectory:nil];
//读取文件的文本信息
NSDictionary *attris = [fileManager attributesOfItemAtPath:path error:nil];
NSLog(@"attris = %@",attris);
//获取大小
NSLog(@"fileSize = %@",attris[NSFileSize]);
简单的文件复制
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = @"/Users/apple/Desktop/show.txt";
NSFileManager *manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:path]){
NSLog(@"文件存在");
}else{
NSString *s = @"hello the world";
[manager createFileAtPath:path contents:[s dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSLog(@"文件创建成功!");
}
NSString *path1 = @"/Users/apple/Desktop/show1.txt";
[self copyfileForm:path to:path1];
}
//当源文件存在的情况下,复制到目标文件
- (void)copyfileForm:(NSString*)filepath1 to:(NSString*)filepath2{
NSFileManager *manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:filepath2]){
NSLog(@"文件存在");
}else{
[manager createFileAtPath:filepath2 contents:nil attributes:nil];
NSLog(@"文件创建成功!");
}
NSFileHandle *origin = [NSFileHandle fileHandleForReadingAtPath:filepath1];
NSFileHandle *new = [NSFileHandle fileHandleForWritingAtPath:filepath2];
NSDictionary *dict = [manager attributesOfItemAtPath:filepath1 error:nil];
NSNumber *filesize = dict[NSFileSize];
NSInteger readedfilesize = 0;
BOOL isEnd = YES;
while (isEnd) {
NSInteger sublength = [filesize integerValue] - readedfilesize;
NSData *data = nil;
if(sublength<100){
data = [origin readDataToEndOfFile];
isEnd = NO;
}else{
data = [origin readDataOfLength:100];
readedfilesize += 100;
[origin seekToFileOffset:readedfilesize];
}
[new writeData:data];
}
[origin closeFile];
[new closeFile];
}
@end