简体   繁体   中英

How to create a new text file to write to in objective c ipad app

How would I go about creating a new text file in my program so that I can write content to it? I already understand how to write content but I haven't been able to find anything (anything easy to understand) on the subject.

Use this method:

+ (id)stringWithContentsOfFile:(NSString *)path
                  usedEncoding:(NSStringEncoding *)enc
                         error:(NSError **)error

An example:

NSString* path = [[NSBundle mainBundle] pathForResource:@"Example"
                                                 ofType:@"txt"];
NSError *error = nil;
NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:&error];

if(error) 
{ 
    NSLog(@"ERROR while loading from file: %@", error);
}

Write to a file works this way: Use this method:

 - (BOOL)writeToFile:(NSString *)path 
          atomically:(BOOL)useAuxiliaryFile 
            encoding:(NSStringEncoding)enc 
               error:(NSError **)error

Example:

[text writeToFile:path atomically:NO  encoding:NSUTF8StringEncoding error:nil];

Mistake:

Can't just write to bundle path. Need to copy it to Documents Directory.

-(void)copyBundleToDocuments
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"];
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"Example.txt"];

        NSError *error;
        BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
        if (success) 
        {
            [text writeToFile:documentPlistPath atomically:NO  encoding:NSUTF8StringEncoding error:nil];
        }
}

Your question is so broad one could drive a truck (or lorry, in UK English) through the middle of it.

But in general, you could add a UITextView to one of your view controllers.

And when you are ready to save, you could take the contents of the text view (which is a NSString), and save it to a file via the NSString writeToFile methods.

And you can load the text view later on via NSString's "initFromFile" method, as long as you know the path to that file.

Here are other questions that people have asked that may help you out.

You can use UITextView to write text

save the file using

     [txtView.text writeToFile:filePath atomically:NO  encoding:NSUTF8StringEncoding error:nil];

Get the TextFile content using

NSString* content = [NSString stringWithContentsOfFile:filePath
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM