मैं अपने आईफोन पर एक टेक्स्ट फ़ाइल में अपना खुद का लॉग लिखना चाहता हूं। मैंने एक त्वरित विधि लिखा है जो एक फाइल को एक स्ट्रिंग लिखता है। अभी यह इसे दस्तावेज़ निर्देशिका में सहेजता है, जो कि डिवाइस पर उतरने के लिए दर्द होने जा रहा है, क्योंकि मैं इसे ब्राउज़ नहीं कर सकता। क्या मैंने इसे लिखने के बाद डिवाइस को तुरंत बंद करने का एक बेहतर तरीका है?उद्देश्य-सी: डिवाइस पर टेक्स्ट फ़ाइल बनाएं और फ़ाइल को आसानी से पुनर्प्राप्त करें
/**
* Logs a string to file
*
* @version $Revision: 0.1
*/
+ (void)logWithString:(NSString *)string {
// Create the file
NSError *error;
// Directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"log.txt"];
// Get the file contents
NSData *localData = [NSData dataWithContentsOfFile:filePath];
if (localData) {
NSString *logString = [[NSString alloc] initWithData:localData encoding:NSUTF8StringEncoding];
string = [logString stringByAppendingFormat:@"%@\n", string];
[logString release];
}
// Write to the file
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}//end
सिडेनोटे- अगर लॉग फ़ाइल लगभग 500 केबी से अधिक हो जाती है या तो यह वास्तव में अक्षम हो जाएगी। केवल परिवर्तन लिखने पर विचार करें (अन्य एपीआई का उपयोग करके) – FeifanZ