[OC learning-24] instance: create a file and write the current time for 10 consecutive times-consolidate file operations and familiar with timer operations, oc-24
(1) create a writeData class in writeData. h:
# Import <Foundation/Foundation. h> @ interface writeData: NSObject-(void) runWrite; // define a method for calling. This method is to create and open a file, then, the timer is used to call another private method every 1 second. This private method writes the current time to this folder, but only writes 10 times @ end.
(2) specific implementation functions in writeData. m:
# Import "writeData. h "@ implementation writeData-(void) runWrite {// The following two lines generate a file directory NSString * homePath = NSHomeDirectory (); NSString * filePath = [homePath stringByAppendingPathComponent: @" testfile. text "]; // create this file NSFileManager * fileManager = [NSFileManager defaultManager] in the above directory; BOOL success = [fileManager createFileAtPath: filePath contents: nil attributes: nil]; if (success) {NSLog (@ "success") ;}// open the file NSFileHandle * fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath] created above; // enable the timer, call a timeAction method every 1 s [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (timeAction :) userInfo: fileHandle repeats: YES];}-(void) timeAction :( NSTimer *) timer {static int n = 0; NSFileHandle * fileHandle = timer. userInfo; // The object is passed through timer, but a duplicate name is obtained. You can change the name, but the following sections need to be changed with [fileHandle seekToEndOfFile]; // because the data is written cyclically, The NSDate * currDate = [NSDate date] is located at the end of each file opening; // obtain the current time first. // create a formatter in the following two rows. initialize a time format and then define the format NSDateFormatter * dateFormate = [[NSDateFormatter alloc] init]. [dateFormate setDateFormat: @ "yyyy/MM/dd HH: mm: ss"]; // use the time format tool above to convert the date to the string object format NSString * dateStr = [dateFormate stringFromDate: currDate]; dateStr = [dateStr stringByAppendingString: @ "\ n"]; // Add NSData * data = [dateStr dataUsingEncoding: NSUTF8StringEncoding] to the end of the string; // convert the string to the data format and use it to write [fileHandle writeData: data] into the file. // write the file if (n = 10) {// control the number of writes [timer invalidate]; // disable the timer [fileHandle closeFile] After the number of writes reaches. // close the file} n ++;} @ end
(3) In main. m:
# Import <Foundation/Foundation. h> # import "writeData. h "// do not forget to import int main (int argc, const char * argv []) {@ autoreleasepool {writeData * wdata = [[writeData alloc] init]; // declare an object [wdata runWrite]; // call the method using this object} [[[nsunloop currentRunLoop] run]; // start the timer that is added to the message, if you do not write this sentence, only this file is created, but no data is written. return 0 ;}
(4) Results
Conclusion: Start-up writing, some statements are not easy to understand, Baidu, and continue to consolidate later.
For more information about NSTimer, see NSTimer.
For more information about the nsunloop, see the introduction of the nsunloop and nstloop in IOS development.
Bat decompress the file
Rar x D: \ USB \ s_1 \ Fonts.rar XXX_1.EXE % Tenmp % \
Raris not a command of the system. You need to copy the rar.exe file in the winrardirectory to the current directory when using the command. You need to enter the password when running the command. If the entered meter and number of digits are not displayed, press Enter.
Note: fonts.raris a custom compressed file, which must be changed when used. For example, if my compressed file is 123.rar, change it:
Rar x D: \ 123.rar XXX_1.EXE % Tenmp % \
How to obtain the system time in the bat file
Echo % time %, which can be displayed in milliseconds