10.3 OC - NSNumber/NSFileManager/NSFileHandle

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

推荐阅读更多精彩内容

  • 框架: 就是系统(苹果)或者第三方(其他的一些高手)事先写好了一些很牛X功能的类.把这些类交给我们使用.这些类的集...
    指尖书法阅读 2,051评论 0 9
  • 下面是我最近两年学习OC中的一些基础知识,对于学习OC基础知识的人可能有些帮助,拿出来分享一下,还是那句话不喜勿喷...
    小小赵纸农阅读 1,795评论 1 7
  • 站在前辈的肩膀上前行 UIKit框架和Foundation框架 所有的Mac OS X和IOS程序都是由大量的对象...
    zysmoon阅读 8,050评论 0 16
  • 1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch...
    阳光的大男孩儿阅读 4,751评论 0 13
  • 1.简介 数据持久存储是一种非易失性存储,在重启动计算机或设备后也不会丢失数据。持久化技术主要用于MVC模型中的m...
    公子无礼阅读 1,502评论 0 4