Objective-c中向plist写入数据的函数
假设我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的公共类对象未实例化,并且您将所有这些消息发送到
nil
。尝试在您调用此方法的应用程序委托代码中记录 cc。您需要创建公共类的实例,例如
(可能会根据您的设置而有所不同)。
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
(may vary depending on your set up).