10.3 OC - NSNumber/NSFileManager/NSFileHandle

NSDictionary

key:OC對象

value:不能是非OC對象

NSNumber

  • 對數(shù)組進行一系列的操作
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

    //創(chuàng)建文件
    NSString *path = @"/Users/frankfan/desktop/demoFile.txt";
    
    [fileManager createFileAtPath:path contents:[@"hello world hellelellelelelelelelelele" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    //創(chuàng)建文件夾
    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(@"文件創(chuàng)建成功!");
    }
    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(@"文件創(chuàng)建成功!");
    }
    
    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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 下面是我最近兩年學習OC中的一些基礎知識,對于學習OC基礎知識的人可能有些幫助,拿出來分享一下,還是那句話不喜勿噴...
    小小趙紙農閱讀 2,815評論 1 7
  • 站在前輩的肩膀上前行 UIKit框架和Foundation框架 所有的Mac OS X和IOS程序都是由大量的對象...
    zysmoon閱讀 8,931評論 0 16
  • 1.項目經(jīng)驗 2.基礎問題 3.指南認識 4.解決思路 ios開發(fā)三大塊: 1.Oc基礎 2.CocoaTouch...
    扶光啟玄閱讀 5,186評論 0 13
  • 1.簡介 數(shù)據(jù)持久存儲是一種非易失性存儲,在重啟動計算機或設備后也不會丟失數(shù)據(jù)。持久化技術主要用于MVC模型中的m...
    公子無禮閱讀 1,773評論 0 4
  • CSS語法 1.格式: 選擇器 { 樣式1;樣式2;} 2.CSS簡單屬性 width:寬度,單位px; heig...
    include_閱讀 170評論 0 0

友情鏈接更多精彩內容