为什么我不能把词典写成plist?

时间:2010-12-01 09:35:26

标签: iphone objective-c cocoa-touch plist nsdictionary

嘿所有,我在这里遇到了一个奇怪的问题 首先,我创建了一个小项目来尝试写字典来实现plist 我声明了一个文本字段和一个按钮,这是关于IBAction的代码:

NSArray *localPathsTemp   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *localDocPathTemp    = [localPathsTemp objectAtIndex:0];
NSString *localFilePathTemp   = [localDocPathTemp stringByAppendingPathComponent:@"InputPassword.plist"];
NSMutableDictionary *localDictreadTemp  = [[NSMutableDictionary alloc] initWithContentsOfFile:localFilePathTemp];
NSMutableDictionary *localSerialNumber = [NSMutableDictionary dictionaryWithObject:setSN.text
                                                                                forKey:@"input_serial_number"];

我尝试记录我在

中写的内容
NSLog(@"Local Dictionary : %@",[localDictreadTemp valueForKey:@"input_serial_number"]);//"input_serial_number=1"
NSLog(@"Dictionary-localSerialNumber is %@",[localSerialNumber valueForKey:@"input_serial_number"]);//1(I enter 1 at textfield)

我觉得这很好吃

但我把这一切都移到了我的项目

我得到了一个N​​ULL

这是我原始项目中的代码(在alertview中)

NSArray *localPathsTemp   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *localDocPathTemp    = [localPathsTemp objectAtIndex:0];
NSString *localFilePathTemp   = [localDocPathTemp stringByAppendingPathComponent:@"InputPassword.plist"];
NSMutableDictionary *localDictreadTemp  = [[NSMutableDictionary alloc] initWithContentsOfFile:localFilePathTemp];
NSMutableDictionary *localSerialNumber = [NSMutableDictionary dictionaryWithObject:setLocalSerialNumber.text
                                                                                        forKey:@"input_serial_number"];
[localDictreadTemp addEntriesFromDictionary:localSerialNumber];
[localDictreadTemp writeToFile:localFilePathTemp atomically:YES];


NSLog(@"Text in the textfield:%@",setLocalSerialNumber.text);
NSLog(@"Local Dictionary %@",localSerialNumber);//"input_serial_number" = 1 
NSLog(@"Dictionary-LocalDictreadTemp %@",[localDictreadTemp valueForKey:@"input_serial_number"]);//NULL

最后的结果是 NULL

这是我无法弄清楚的问题,我在样本中得到了一个值,但在这里不起作用。

为什么......我给出了错误的文件路径或者......?

感谢所有回复


有关我的项目的更多信息

我的应用程序用于控制硬件设备
如果用户是第一次使用申请
用户必须输入密码和设备序列号
该设备使用bonjour搜索设备
并从URL获取默认序列号/密码(此案例在WIFI中)
例如,网址为http://11.33.44.65/defaultsn_pwd.php

我会得到一个plist文件,如:

{
     sn  = 1; 
     pwd = 777;
}

这是用户无法看到的信息。
应用程序将弹出一个带有两个文本字段的alertView 如果textfield中的文本与sn / pwd表单URL匹配 它会写入。
但是!!!如果用户在3G环境中,它将连接到我的服务器来控制设备
这与Wifi非常不同,因为您必须为服务器提供设备序列号

所以

  1. 输入序列号(我需要在iphone上写plist,但在这里得到NULL)

  2. 将序列作为新网址的一部分,例如http://server/defaultsn_pwd.php ?sn = 1

  3. 因为我无法在plist中编写序列号,所以我无法获得正确的URL 我的网址会这样http://server/defaultsn_pwd.php ?sn = NULL
    它会崩溃我的程序

    这是一个非常致命的问题,请帮我解决这个问题....

    我陷入这个问题太久了......

1 个答案:

答案 0 :(得分:1)

NSMutableDictionary *localDictreadTemp  = [[NSMutableDictionary alloc] initWithContentsOfFile:localFilePathTemp];

我认为这是问题。除非您在代码中的其他位置将文件写入该位置,否则localFilePathTemp处没有文件。由于该文件为空,您的initWithContentsOfFile会返回nil字典,其余的是nil对象的连锁反应。

试试这个:

if ([[NSFileManager defaultManager] fileExistsAtPath:localFilePathTemp]) {
    NSLog(@"file exists at %@",localFilePathTemp);
} else NSLog(@"Some dude from the internet solved my problem, I should probably go choose his answer");
相关问题