KVO enables display of custom file copy progress and kvo custom progress
Description: Custom file class. File Creation and copy are implemented through NSFileManager and NSFileHandle. To control the concurrent memory usage, you can control the fixed length of each assignment to replicate multiple times:
NSString * path = NSHomeDirectory (); path = [path stringByAppendingPathComponent: @ "deskTop/Boby. m "]; NSString * target = NSHomeDirectory (); target = [target stringByAppendingPathComponent: @" deskTop/target. m "]; NSFileManager * manager = [NSFileManager defaultManager]; // check and create a file if (! [Manager fileExistsAtPath: path]) {[manager createFileAtPath: path contents: nil attributes: nil] ;}if (! [Manager fileExistsAtPath: target]) {[manager createFileAtPath: target contents: nil attributes: nil];} NSDictionary * dic = [manager attributesOfItemAtPath: path error: nil]; NSFileHandle * handle = [NSFileHandle fileHandleForReadingAtPath: path]; NSFileHandle * handletTarget = [NSFileHandle handle: target]; int total = (int) [dic [@ "NSFileSize"] integerValue]; self. totalSize = total; int per = 50; Int count = total % per = 0? Total/per: total/per + 1; for (int I = 0; I <count; I ++) {[handle seekToFileOffset: self. nowSize]; NSData * data = [handle readDataOfLength: per]; int tem = per * (I + 1); if (tem> total) {tem = total;} self. nowSize = tem; [handletTarget seekToEndOfFile]; [handletTarget writeData: data]; [NSThread sleepForTimeInterval: 0.2];} [handle closeFile]; [handletTarget closeFile];
Description: Custom Users can dynamically observe the progress of the current file copy by setting the observer and display it to the console or output it to the UI. It also provides method interfaces to start file copy.
-(Id) initWithFile :( FileMake *) files {self = [super init]; if (self) {self. file = files; [self. file addObserver: self forKeyPath: @ "nowSize" options: NSKeyValueObservingOptionNew context: nil];} return self;}-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {CGFloat all = self. file. totalSize; CGFloat now = [[change objectForKey: @ "new"] floatValue]; CGFloat result = now/all; NSLog (@ "%. 2f ", result); // you must not forget to destroy the current observer if (result = 1) {[self. file removeObserver: self forKeyPath: @ "nowSize"] ;}}- (void) begin {[self. file startCopy];}