Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rahul29tech

macrumors member
Original poster
Dec 22, 2011
41
0
I use this 3 methods to enter text i enter on textfields, and after entering data in textfields press home button on simulator and then quit application and when again launch application so textfield was again empty,means data is not entered in plist?/ y is it so ,??? m not making any plist file manually,whatever did is just in this 3 methods
1)
Code:
-(NSString *)dataFilePath
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory=[paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];  
}

2)
-(void) applicationWillTerminate:(NSNotification *)notification
{
    
    
    NSMutableArray *array=[[NSMutableArray alloc] init];
    [array addObject:field1.text];
    [array addObject:field2.text];
    [array addObject:field3.text];
    [array addObject:field4.text];
   
    
    [[array description] writeToFile:[self dataFilePath] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
    
    
       
   // [array release];
}

3)
- (void)viewDidLoad
{
    NSString *filePath=[self dataFilePath];
    NSLog(@"%@",filePath);
        if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSArray *array=[[NSArray alloc] initWithContentsOfFile:filePath];
        field1.text=[array objectAtIndex:0];
        field2.text=[array objectAtIndex:1];
        field3.text=[array objectAtIndex:2];
        field4.text=[array objectAtIndex:3];
        [array release];
    }
    
    UIApplication *app=[UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
    [super viewDidLoad];
}
 
Last edited by a moderator:
What I notice is that you're writing your array's description (an NSString, if I'm not mistaken) but then reading in an NSArray.

I haven't used documents in a long time, and I'm curious why you're not just using [[NSUserDeafaults standardUserDefaults].
 
What I notice is that you're writing your array's description (an NSString, if I'm not mistaken) but then reading in an NSArray.

This is 100% the problem. If the OP tested the loaded array to see if it was nil in 3) before trying to use it to populate anything they would see that it was nil indicating a problem loading the array from it's file representation. This should always be done when you load something off disk: it's basic error checking.
 
Sir, so plz give the code by which i can save data which i type in text field is go & saved to plist files and when quit and then again rebuild application so that data shown in textfields....

and plz done it without using sqlite and archives plz done it in simple way???

thanks in advance sirr
 
Sir, so plz give the code by which i can save data which i type in text field is go & saved to plist files and when quit and then again rebuild application so that data shown in textfields....

and plz done it without using sqlite and archives plz done it in simple way???

thanks in advance sirr

We're here to help, not do it for you.

Part of learning is doing it yourself.

You should either:
A.) Change what you're storing to an NSArray.
B.) Change what you're reading to an NSString.
 
sry sir...and thanks....:) but its frusted me alot..:( :(......sir as shown in my code that i am not using nsuserdufine, so how to save string in plist withoust using nsuer define ,archiver and withour sqlite, as i did in in this code is it possible sir???
 
What? I didn't bring up NSUserDefaults in my second post on this topic... look, just change

Code:
[[array description] writeToFile:[self dataFilePath] atomically:YES encoding:NSUTF8StringEncoding error:NULL];

to

Code:
[array writeToFile:[self dataFilePath] atomically:YES];

That should work. Your issue is that you were doing this:

On launch: Open the NSArray that's in the file.
On close: Write an NSString in the file.

Thus it wasn't working because when you launched your program, it was looking for an NSArray but finding an NSString instead.
 
Yes sir,u pick my doubt....

i.e this is what i actually want

On launch: Open the NSArray that's in the file.
On close: Write an NSString in the file.

Thus it wasn't working because when you launched your program, it was looking for an NSArray but finding an NSString instead.

Code:
[array writeToFile:[self dataFilePath] atomically:YES];

Sir, i used this line also but not works,run but not got my results ??? plz tell me sir , how i solved this problem when i launched my program, it was looking for an NSArray but finding an NSString instead.so how i can find nsarray not to nsstring???

thanks a lot for understanding me...:(
 
Last edited by a moderator:
You said it runs, what steps have you taken to try and isolate the problem? Are any warnings outputted by the compiler? Have you tried walking through it with the debugger to see exactly what values are in the array prior to writing it out, checked the return value of writeToFile:atomically: to see if it was actually written out, or examined the file that's written out?
 
Last edited by a moderator:
okh sir,next time i dont do double post...

Code:
[array writeToFile:[self dataFilePath] atomically:YES];

sir,when i use debugging so found this above line is not compile bcoz, i call this method in

Code:
UIApplication *app=[UIApplication sharedApplication];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

as shown in my 1st post, and no any file of name data.plist is generated in simulator's document folder???? and i do all those which is written in book still nt achived target??? thanks :(

----------

program exists before reaching to that line


Code:
[array writeToFile:[self dataFilePath] atomically:YES];


2nd thing i wana to ask is that is it possible by this whatever i post here, that to save data permanently and when application rebuild and run so got previously saved data????
 
Last edited by a moderator:
Thanks a lot to every1 ,i got my mistake but not still reaches to the solution....

problem is my applicationWillTerminate: method is not calling by :-

Code:
UIApplication *app=[UIApplication sharedApplication];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

now plz help me y above line is unable to call my applicationWillTerminate method???
 
Last edited by a moderator:
Three bits of advice regarding asking for help:

1.) Don't email your questions to us. People offer help through the forums; our inboxes aren't the place to send questions that you've already posted on the forums.
2.) Don't make back-to-back posts. This is called double posting. If you'd like to add more information to a post before anyone has responded to it, use the edit button. (This seems like something that would be really easy to automatically make forum servers handle... Why don't they?)
3.) Use code tags around your code. Put the word code inside of []'s before your code, and /code inside of []'s at the end of your code. Do that for every file you share.

Finally, the answer to your question is this:

When the home button is pressed in iOS 4 and newer, the active application resigns or goes to the background or something like that. It does not terminate like it did in iOS 3 and prior. I don't remember the name of the exact notification you need to register for, but if you look at the Apple dev docs at the notifications UIApplications put out, you can find it. (Google "UIApplication Notifications". I haven't tried myself but I'm sure one of the first links will tell you what you need to know.)
 
thanks a lot sir....m new here so not know rules but very soon m familiar with all...

thanks problem is solved ...:) via ur help that my application not killing while pressing home button so nw i kill it expicitly thankss sir....:)
 
thanks a lot sir....m new here so not know rules but very soon m familiar with all...

MOD NOTE: When you signed up for an account on these forums, you agreed to follow the Rules. Please be sure to review them before you post again. If you have any questions about the rules, you can start a thread in the Site & Forum Feedback forum or you can contact the admins, via the Contact Us link that is at the bottom of every page here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.