为什么我不能将字典写入 plist?

发布于 2024-10-05 07:54:41 字数 3096 浏览 3 评论 0 原文

大家好,我遇到了一个奇怪的问题
首先,我创建一个小项目来尝试将字典写入 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)

正如我所想,这很好

但我将这整个移动到我的项目中

我得到了 NULL

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

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中)
例如,URL 是 http://11.33.44.65/defaultsn_pwd.php

我将得到一个 plist 文件,如下所示:

{
     sn  = 1; 
     pwd = 777;
}

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

所以

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

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

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

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

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

Hey All,I got a weird problem here
First I create a small project to try write dictionary to plist
I declared a textfield and a button and this is code about 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"];

I try to log what I write in

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)

It's woks fine as I think

But I move this whole to my project

I got a NULL

This is the code in my original project(in a 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

Last result is NULL

That's the problem I can't figure out,I got a value at sample ,but not work here .

Why...did I give a wrong file path or ... ?

Thanks for all replys


More information about my project

My application is use to control a hardware device
If the user is first time to use application
User have to enter the password and device serial number
The device is use bonjour to search the device
And get a default serial number/password from URL(this case is in WIFI)
ex.the URL is http://11.33.44.65/defaultsn_pwd.php

I will get a plist file like :

{
     sn  = 1; 
     pwd = 777;
}

This is the information user can't see it.
And the application will pop up an alertView with two textfields
If the text in the textfield is match the sn/pwd form URL
It will write in.
But !!!if user is in the 3G environment , It will connect to my server to control the device
This is very different with Wifi,because you have to give server a device serial number

So

  1. Enter the serial number(I need to write in plist on iphone,But got NULL here)

  2. Take the serial as a part of a new URL ex.http://server/defaultsn_pwd.php?sn=1

Because I can't write serial number in plist ,so I can't get a right URL
My URL will like this http://server/defaultsn_pwd.php?sn=NULL
It will crash my program

This is avery deadly issues ,Please help me to figure this problem out....

I trapped in this problem too long...

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

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

发布评论

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

评论(1

樱桃奶球 2024-10-12 07:54:41
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");
NSMutableDictionary *localDictreadTemp  = [[NSMutableDictionary alloc] initWithContentsOfFile:localFilePathTemp];

I betcha this is the problem line. Unless you are writing a file to that location elsewhere in code, there is no file at localFilePathTemp. Since that file is empty, your initWithContentsOfFile returns a nil dictionary, and the rest is a chain reaction of nil objects.

try this:

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