objective-c - Plist不会写入文件

标签 objective-c cocoa filesystems plist

当我尝试这段代码时:

if ([[NSFileManager defaultManager] fileExistsAtPath:@"~/Library/Application Support/Staying Afloat/stats/static-stats.plist"] == NO) {
    NSMutableDictionary* tempDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                     [[NSString alloc] initWithString:@"0"],@"completedGames",
                                     [[NSString alloc] initWithString:@"0"],@"floatsCollected",
                                     [[NSString alloc] initWithString:@"0"],@"sec",
                                     [[NSString alloc] initWithString:@"0"],@"subScore",
                                     [[NSString alloc] initWithString:@"0"],@"highScore",
                                     [[NSString alloc] initWithString:@"0"],@"longestSec", nil];

    [tempDict writeToFile:@"~/Library/Application Support/Staying Afloat/stats/static-stats.plist" atomically:YES];

    NSLog(@"written file");

}

输出为

2012-04-14 19:15:10.009 Staying Afloat[3227:9c07] written file

所以循环已运行,但文件未写入? 谁能指出我将 plist 保存到非本地化位置的正确方向?

编辑:这是针对 mac 的

最佳答案

写入时需要使用 -(NSString *)stringByExpandingTildeInPath 扩展路径并检查文件是否存在。

编辑:阅读 NSDictionary 文档中的 writeToFile:automatically 方法。它说

If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.

所以只需做类似的事情

[tempDict writeToFile:[[NSString stringWithString:@"~/Library/Application Support/Staying Afloat/stats/static-stats.plist"] stringByExpandingTildeInPath] atomically:YES];

关于objective-c - Plist不会写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10151865/

相关文章:

iphone - 从异步更新文本字段返回 - iPhone

iphone - 删除某个文件夹中的所有文件和文件夹

cocoa - 如何以编程方式展开和折叠 NSSplitView 的部分内容?

ios - addSubview 上的 Objective-C 内存泄漏

objective-c - 检查对象是否符合 Objective-C 中的两个独立协议(protocol)

ios - 带有标题 View 的 UITableView 不滚动到顶部

java - 我可以使用 JNDI 访问文件/其内容吗?

ios - 在 iOS 上启动带有视频的 youtube 应用程序

linux - ext4 卷的根目录上的 fsync 会刷新所有子卷吗?

node.js - Nodejs写入文件