ios_NSFileManager/NSData/NSDate

程序设计模式

  • 组合模式,(聚合模式)
  • 单利模式(静态变量,defaultFileManager)
  • 策略模式(封装变化点, block)
  • 向上抽象(协议,protocol)
  • 享元(NSString池,同一对象同一地址)

NSFileManager

  • defaultFileManager: 单利模式
  • 文件属性获取(根据NSDictionary获取):

    - (nullable NSDictionary<NSFileAttributeKey, id> *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

  • 目录下文件与子目录获取:
  • - (nullable NSArray<NSString *> *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));    //第一级子目录

  • - (nullable NSArray<NSString *> *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));   // 所有子目录

  • 创建目录
  • - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSFileAttributeKey, id> *)attributes error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

  • 移动目录
  • - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

  • 删除目录(目录不必为空)
  • - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

        //  folder path
        NSString * docPath = @"/Users/dude/Desktop";
        //  filePath
        NSString * filePath = @"/Users/dude/Desktop/myTestFile/dudeFile.txt";
        
        //  get file property
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSError * error = nil;
        NSDictionary * dic = [fileManager attributesOfItemAtPath:filePath error:&error];
        if(error == nil){
            NSLog(@"dic: %@", dic);
        }else{
            NSLog(@"error: %@", error);
        }
//        dic: {
//            NSFileCreationDate = "...";
//            NSFileExtendedAttributes =     {
//                "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;
//            };
//            NSFileExtensionHidden = 0;
//            NSFileGroupOwnerAccountID = 20;
//            NSFileGroupOwnerAccountName = staff;
//            NSFileHFSCreatorCode = 0;
//            NSFileHFSTypeCode = 0;
//            NSFileModificationDate = "...";
//            NSFileOwnerAccountID = ...;
//            NSFileOwnerAccountName = ...;
//            NSFilePosixPermissions = 420;
//            NSFileReferenceCount = 1;
//            NSFileSize = 10;
//            NSFileSystemFileNumber = ....;
//            NSFileSystemNumber = ....;
//            NSFileType = NSFileTypeRegular;
//        }
        
        
        //  get files/sub-document list in folder
        NSArray * array = [fileManager contentsOfDirectoryAtPath:docPath error:&error];
        NSLog(@"array: %@", array);
//        array: (
//                ".DS_Store",
//                myTestFile,
//                ".localized",
//                "IOS Program"
//                )
     




//  create folder
        /*withIntermediateDirectories: YES 逐级创建文件夹; NO 只创建一级目录
         */
        BOOL isCreate = [fileManager createDirectoryAtPath:@"/Users/dude/Desktop/myTestFile/subFolder" withIntermediateDirectories:YES attributes:nil error:&error];
        if(isCreate){
            NSLog(@"success creating");
        }else{
            NSLog(@"error: %@", error);
        }



//  move folder
        NSString * newPath = @"/Users/dude/Desktop/myTestFile/newFolder";   //  move item in old path to new path(not exit before), remove old path
        BOOL successMov = [fileManager moveItemAtPath:@"/Users/dude/Desktop/myTestFile/subFolder" toPath:newPath error:&error];
        if(successMov){
            NSLog(@"success Move");
        }else{
            NSLog(@"error: %@", error);
        }




    //    remove folder
    BOOL isRemove = [fileManager removeItemAtPath:@"/Users/dude/Desktop/myTestFile/" error:&error];
        if(isRemove){
            NSLog(@"success removing");
        }else{
            NSLog(@"error: %@", error);
        }
  • 复制文件
  • - (BOOL)createFileAtPath:(NSString *)path contents:(nullable NSData *)data attributes:(nullable NSDictionary<NSFileAttributeKey, id> *)attr;

  • 移动文件
  • - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

  • 删除文件
  • - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

        NSFileManager * fileManager = [NSFileManager defaultManager];
        
        //  manage file
        NSString * filePath = @"/Users/dude/Desktop/newFolder/myfile.txt";


        //  get file data, copy file
        NSData * data = [NSData dataWithContentsOfFile:filePath];
        NSLog(@"data: %@", data.description);
        BOOL isCreate = [fileManager createFileAtPath:@"/Users/dude/Desktop/newFolder/myfile(1).txt" contents:data attributes:nil];
        if(isCreate){
            NSLog(@"success creating");
        }else{
            NSLog(@"failed creating");
        }



//  move  file
        BOOL isMove = [fileManager moveItemAtPath:@"/Users/dude/Desktop/newFolder/myfile(1).txt"  toPath:@"/Users/dude/Desktop/myfile(1).txt"  error:&error];
        if(isMove){
            NSLog(@"success moving");
        }else{
            NSLog(@"error: %@", error);
        }



//  remove file, file should exited
        BOOL isRemove = [fileManager removeItemAtPath:@"/Users/dude/Desktop/myfile(1).txt"  error:&error];
        if(isRemove){
            NSLog(@"success remove");
        }else{
            NSLog(@"error: %@", error);
        }

NSData/NSMutableData

 处理音频文件、文本文件、视频文件

//  file getting and writing
        NSData * fileData = [NSData dataWithContentsOfFile:filePath];
        BOOL isWrite = [fileData writeToFile:@"/Users/dude/Desktop/newFolder/myfile(1).txt" atomically:YES];
        NSLog(@"%@", isWrite ? @"success write" : @"failed writing");
        
        NSString * str1 = @"one";
        NSString * str2 = @"dude";

        NSData * data1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
        NSData * data2 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
        
        NSMutableData *  mutableData = [[NSMutableData alloc] init];
        [mutableData appendData:data1];
        [mutableData appendData:data2];
        [mutableData appendData:data1];
        isWrite = [mutableData writeToFile:filePath atomically:YES];
        NSLog(@"%@", isWrite ? @"success write" : @"failed writing");

NSDate

  • 日期获取
  • 日期比较
  • 日期格式
  • 时区设置
        NSDate * date = [[NSDate alloc] init];
        NSLog(@"date: %@", date);
        
        NSDate * date1 = [NSDate date];
        NSLog(@"date1: %@", date1);
//        date1: Sat May 11 16:58:03 2019
        
        NSTimeInterval seconds = 24 * 60 * 60;
        NSDate * date2 = [date1 dateByAddingTimeInterval:seconds];
        NSLog(@"date2: %@", date2);
//         date2: Sun May 12 16:58:03 2019




        //  compare dates
        BOOL isEqual = [date1 isEqualToDate:date2];
        NSLog(@"%@", isEqual ? @"isEqual" : @"notEqual");
        
        NSDate * earierDate = [date1 earlierDate:date2];
        NSLog(@"earierDate: %@", earierDate);
        
        NSDate * laterDate = [date1 laterDate:date2];
        NSLog(@"laterDate: %@", laterDate);



        //deal with time zones issue
        NSDate * date = [NSDate date];
        NSDateFormatter * dateFormetter = [[NSDateFormatter alloc] init];
        [dateFormetter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
        NSString * str = [dateFormetter stringFromDate:date];
        NSLog(@"str: %@", str);//str: 2019/05/11 05:21:39
        
        [dateFormetter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        str = [dateFormetter stringFromDate:date];
        NSLog(@"str: %@", str);//str: 2019/05/11 09:21:39




        //  NSDate -> NSString, show with format
        NSDate * date = [NSDate date];
        NSDateFormatter * dateFormetter = [[NSDateFormatter alloc] init];
        [dateFormetter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
        // yyyy: year;  MM: month;  dd: day;    hh: hour;   mm:minute;  ss:second
        NSString * str = [dateFormetter stringFromDate:date];
        NSLog(@"str: %@", str);//str: 2019/05/09 05:09:53
        
         
        //  NSString with format -> NSDate
        NSString * str = @"2019/01/25 00:00:00";
        NSDateFormatter * formater = [[NSDateFormatter alloc] init];
        [formater setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
        NSDate * date = [formater dateFromString:str];
        NSLog(@"date: %@", date);//date: Fri Jan 25 00:00:00 2019

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值