Objective-c中向plist写入数据的函数

发布于 2024-12-08 20:31:30 字数 1993 浏览 0 评论 0原文

假设我有一个名为 savePlist 的自定义函数,如下所示。

CommonClass.m

- (NSString *)getDirectoryPath
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathList objectAtIndex:0];
    return path;
}

- (void)savePlist:(NSString *)fileName WithArray:(NSArray *)fileArr
{
    NSString *path = [[self getDirectoryPath] stringByAppendingPathComponent:fileName];
    [fileArr writeToFile:path atomically:YES];
}

当我运行如下代码时,没有生成 plist。

ABCAppDelegate.m

...
[cc savePlist:@"example.plist" WithArray:[NSArray new]];

example.plist 文件尚未生成,我的代码是否有错误?

谢谢

更新:

如果我使用下面的代码,xxx.plist 文件已成功生成。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *plistFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"abc.plist"];
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile];

if (!plist) {
    plist = [NSMutableDictionary new];
    [plist writeToFile:plistFile atomically:YES];
    NSLog(@"write plist");
}

参考: link

更新2:

我将代码更改如下:

NSLog(@"code");
[cc savePlist:@"example.plist" WithArray:[NSArray new]];
NSLog(@"code");

- (void)savePlist:(NSString *)fileName WithArray:(NSArray *)fileArr
{
    NSLog(@"fileName = %@, fileArr = %@", fileName, fileArr);
    NSString *path = [[self getDirectoryPath] stringByAppendingPathComponent:fileName];
    NSLog(@"path = %@", path);
    [fileArr writeToFile:path atomically:YES];
}

它只是打印出:

2011-10-10 14:24:00.560 ABC[3390:207] code
2011-10-10 14:24:00.561 ABC[3390:207] code

没有fileName =和fileArr =的消息,也在日志上打印出path =,所以savePlist里面的代码没有到过被处决?

Suppose I have a custom function called savePlist as below.

CommonClass.m

- (NSString *)getDirectoryPath
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathList objectAtIndex:0];
    return path;
}

- (void)savePlist:(NSString *)fileName WithArray:(NSArray *)fileArr
{
    NSString *path = [[self getDirectoryPath] stringByAppendingPathComponent:fileName];
    [fileArr writeToFile:path atomically:YES];
}

While I run a code as below, no plist is generated.

ABCAppDelegate.m

...
[cc savePlist:@"example.plist" WithArray:[NSArray new]];

The file of example.plist has not been generated, is there any mistakes on my code?

Thanks

UPDATE:

If I use the code as below, the xxx.plist file has been generated successfully.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *plistFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"abc.plist"];
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile];

if (!plist) {
    plist = [NSMutableDictionary new];
    [plist writeToFile:plistFile atomically:YES];
    NSLog(@"write plist");
}

Reference: link

UPDATE 2:

I change the code as below:

NSLog(@"code");
[cc savePlist:@"example.plist" WithArray:[NSArray new]];
NSLog(@"code");

- (void)savePlist:(NSString *)fileName WithArray:(NSArray *)fileArr
{
    NSLog(@"fileName = %@, fileArr = %@", fileName, fileArr);
    NSString *path = [[self getDirectoryPath] stringByAppendingPathComponent:fileName];
    NSLog(@"path = %@", path);
    [fileArr writeToFile:path atomically:YES];
}

It just print out:

2011-10-10 14:24:00.560 ABC[3390:207] code
2011-10-10 14:24:00.561 ABC[3390:207] code

No message of fileName = and fileArr =, also path = print out on the log, so the code inside savePlist have not been executed?

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

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

发布评论

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

评论(1

三寸金莲 2024-12-15 20:31:30

看起来您的公共类对象未实例化,并且您将所有这些消息发送到nil。尝试在您调用此方法的应用程序委托代码中记录 cc。

您需要创建公共类的实例,例如

cc = [[CommonClass alloc] init];

(可能会根据您的设置而有所不同)。

It looks like your common class object is not instantiated, and you are sending all those messages to nil. Try logging cc in your app delegate code, where you call this method from.

You need to create an instance of your common class, something like

cc = [[CommonClass alloc] init];

(may vary depending on your set up).

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