Why can't I write Dictionary to plist?

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...

Answers


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");

Need Your Help

TCP concurrent echo server unable to read string from first client

c linux sockets tcp

I am implementing a simple TCP process-per-client concurrent echo server. The server is unable to read string from first client. However for further clients, the server echoes back the string corre...

Possible issue with running selenium tests on one machine concurrently

selenium webdriver selenium-webdriver

I have multiple similar sites (same layout, just different data), and each of them has drop down menu on mouse over (and disappears on mouse out).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.