[OC learning-24] instance: create a file and write the current time for 10 consecutive times-consolidate the file operation and familiarize yourself with the timer operation

Source: Internet
Author: User

(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.

[OC learning-24] instance: create a file and write the current time for 10 consecutive times-consolidate the file operation and familiarize yourself with the timer operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.