字符串未写入文件 - iPhone

发布于 2024-11-19 05:11:10 字数 512 浏览 3 评论 0原文

我在 iPhone 中使用以下代码将字符串写入存储在我的 iPhone 项目资源文件夹中的文件。当我尝试读取它时,它成功读取数据,但当我尝试写入时,它不写入文件,尽管它也没有给我任何错误。

这是我的代码:

NSString *myString; //Assuume the string you want to write is this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
[myString writeToFile:path atomically:YES];

请任何人指导。

I am using the following code in iPhone to write a string into the file that is stored in my iPhone Project Resource Folder. When i try to read its reading the data successfully but when i try to write its not writing the file although its aloso not giving me any error.

this is my code:

NSString *myString; //Assuume the string you want to write is this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
[myString writeToFile:path atomically:YES];

Please can anybody guide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2024-11-26 05:11:10
- (void) testStringReadWrite {
    NSString *myString = @"abcd efgh"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
    CFShow(path);
    NSError *error = nil;

    [myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"%@",error);
    }


    NSString *readString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"%@", error);
    } else {
        CFShow(readString);
    }

}
- (void) testStringReadWrite {
    NSString *myString = @"abcd efgh"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
    CFShow(path);
    NSError *error = nil;

    [myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"%@",error);
    }


    NSString *readString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"%@", error);
    } else {
        CFShow(readString);
    }

}
你的背包 2024-11-26 05:11:10

writeToFile:atomically: 在 iOS 2.0 中已被弃用。使用 writeToFile:atomically:encoding:error: 代替。您应该检查字符串的内容,例如通过 NSLog() 函数。不适当的初始化可能会导致失败。

NSLog(@"%@", myString);
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

The writeToFile:atomically: has been depredecated in iOS 2.0. Use writeToFile:atomically:encoding:error: instead. You should check the content of the string, for example by NSLog() function. Inappropriate initialization can cause a failure.

NSLog(@"%@", myString);
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
桃扇骨 2024-11-26 05:11:10

只是我的两分钱,来自 OS X,我习惯于调用 writeToURL() 而不是 writeToFile()。事实证明,writeToFile() 是适用于 iOS 的。

Just my two cents, coming from OS X, I am used to calling writeToURL() instead of writeToFile(). Turns out, writeToFile() is the one that worked for iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文