【发布时间】:2013-07-02 01:49:22
【问题描述】:
我调查了其他用户类似的问题,但我仍然找不到我的代码的问题。
这就是问题所在。
当应用程序启动时,我调用一个名为 setupDirectory 的方法,该方法创建(或根据需要从应用程序包复制)一个用于名称的 plist 和一个用于配置的 plist:
@implementation Data
static NSString * NamePlist;
static NSString * ConfigPlist;
+(void) setupDirectory
{
//Names
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * dataPath = [paths objectAtIndex:0];
dataPath = [dataPath stringByAppendingPathComponent:@"NamesPlist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dataPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"NamesPlist" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:dataPath error:nil];
}
//Config
NSString *configPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
configPath = [configPath stringByAppendingPathComponent:@"ConfigPlist.plist"];
if (![fileManager fileExistsAtPath:configPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"ConfigPlist" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:configPath error:nil];
}
NamePlist = dataPath;
ConfigPlist = configPath;
}
然后当我需要添加名称时,我调用 Add name 方法将名称添加到 plist:
+(void) addName: (NSString *) name andConfigs: (NSArray *) configs
{
// Name
// Get the old content of the plist file
NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:Namelist];
// Copy the old content of the plist file to a new dictionary
NSMutableDictionary * newContent = [oldContent mutableCopy];
//Add the new config to the dictionary
[newContent setObject:[NSNull null] forKey:name];
// Set the new dictionary as the plist file
[newContent writeToFile:NamePlist atomically:YES];
//Configs
oldContent = [NSDictionary dictionaryWithContentsOfFile:ConfigPlist];
// Copy the old content of the plist file to a new dictonary
newContent = [oldContent mutableCopy];
//Add the new config to the dictionary
[newContent setObject:configs forKey:name];
// Set the new dictionary as the plist file
[newContent writeToFile:ConfigPlist atomically:YES];
}
但是当我需要从 plist 中获取信息时,我什么也没得到,所以它没有写?
+(NSArray *) getAllNames
{
NSDictionary * names = [NSDictionary dictionaryWithContentsOfFile:NamePlist];
NSArray * allNames = [(NSArray*) [names allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
return allNames;
}
GetallNames 返回一个空数组。
对不起,如果这个问题已经回答了,但我真的找了,仍然不知道出了什么问题
编辑 1:
我使用 writeToFile 的返回值来检查是否有任何失败。 我对来自 configsPlist 的 writeToFile 得到了肯定,但对来自 NamesPlist 的 writeToFile 得到否定。 所以我认为配置工作正常,但不是 namesPlist
编辑 2:
使用调试器我注意到![fileManager fileExistsAtPath:configPath] 和!fileManager fileExistsAtPath:dataPath] 是返回NO 所以我实际上并没有进入这些if 语句。
解决方案
好的,我又给了它一次机会,并尝试使用字符串而不是 [NSNull null] 创建,现在它可以工作了...我想我应该使用 [NSNull null] 创建带有空对象的字典
【问题讨论】:
-
很难在不每 15 秒进行一次编辑的情况下仔细阅读所有代码。
-
抱歉,修正了一些错别字。我现在完成了
-
利用
writeToFile:atomically:方法和copyItemAtPath:toPath:error:方法的返回值来查看是否有任何失败(并使用错误参数查看原因)。 -
您应该考虑使用 NSFileManager 方法 URLsForDirectory:inDomains: 和 URLForDirectory:inDomain:appropriateForURL:create:error:。它返回 URL,这是首选格式。有关文件系统实用程序的更多信息,请参阅文件系统编程指南:developer.apple.com/library/ios/#documentation/FileManagement/…
-
我对来自 configsPlist 的 writeToFile 的结果为“是”,但对来自 NamesPlist 的 writeToFile 的结果为“否”。所以我认为配置工作正常,但不是 namesPlist
标签: ios objective-c writetofile