为什么我的 [字典对象 ForKey:@ "TOSAcceptedValue" ] 为空?



我正在写入和读取文档目录中的plist。首先,如果没有找到文件,我将文件写出来。这似乎可以正常工作,因为起初那里没有文件,现在有了,双击文件会显示一个名为TOSAcceptedValue的键的预期内容,值为no。

但是,如果找到文件,这发生在我运行上述一次之后,我试图读取相同的值,我得到一个空值。这是代码,它可能不是很漂亮,因为我已经修改了一段时间才使它正常工作。

NSError *error;
NSString *TOSAcceptedStatus ;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSLog( @"paths is %@", paths);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"AppUsage.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
//first see if file exists. if it doesn't then write it out with a value of NO for Terms of Use Accepted
if (![fileManager fileExistsAtPath: path])
{                
    NSMutableDictionary *appUsageNo = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //here add element to data file and write data to file
    NSString *value = @"NO";
    [appUsageNo setObject:@"TOSAcceptedValue" forKey:value];
    [appUsageNo writeToFile: path atomically:YES];
    [appUsageNo release]; 
    //and set TOSAcceptedStatus to no since we know its a no right now
    TOSAcceptedStatus = @"NO";
}
else { //file was found at expected location so let's see if they accepted Terms of Use already
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    NSLog( @"path is %@", path);
    TOSAcceptedStatus = [dictionary objectForKey:@"TOSAcceptedValue"];
    //NSLog(@"TOSAcceptedStatus is %@", TOSAcceptedStatus);
    NSLog(@"TOSAcceptedStatus is %@", [dictionary objectForKey:@"TOSAcceptedValue"]);
    [dictionary release];
}

这是我的控制台结果

2011-09-09 21:47:23.177 myApp[1027:207] paths is (
    "/Users/user1/Library/Application Support/iPhone Simulator/4.3/Applications/34562D85-DBE9-4A4F-A142-JEFC1F4808D1/Documents"
)
2011-09-09 21:47:44.915 myApp[1027:207] path is /Users/user1/Library/Application Support/iPhone Simulator/4.3/Applications/34562D85-DBE9-4A4F-A142-JEFC1F4808D1/Documents/AppUsage.plist
2011-09-09 21:47:50.448 myApp[1027:207] TOSAcceptedStatus is (null)

有线索为什么我不能得到TOSAcceptedStatus回来?

另外,我允许写入和读取应用程序启动后的plist ?

我认为你的主要问题是你把对象和键放反了:

[appUsageNo setObject:value forKey:@"TOSAcceptedValue"];

相关内容

  • 没有找到相关文章

最新更新