Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2012, 06:12 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 38
iOS.Lover is on a distinguished road
Default NSUserDefaults and save string with date

I trying to save a text with NSUserDefaults but my problem is I don't know how can store data on a specific date , I crate a custom calendar app which users can store their daily notes on it , so for example users save some text in FEB 3 then they need write something else in another date , how can I store these text on different dates ? here is my code :

Code:
/retriveData :

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *saving = [defaults objectForKey:@"saving"];
    saveTextToday.text = saving;


- (void) saveNotes{


        NSString *retriveData = [saveTextToday text];
        NSUserDefaults *defaul = [NSUserDefaults standardUserDefaults];
        [defaul setObject:retriveData forKey:@"saving"];

        [defaul synchronize];


}

Last edited by iOS.Lover; 02-03-2012 at 06:20 AM.
iOS.Lover is offline   Reply With Quote
Old 02-03-2012, 06:47 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iOS.Lover View Post
I trying to save a text with NSUserDefaults but my problem is I don't know how can store data on a specific date , I crate a custom calendar app which users can store their daily notes on it , so for example users save some text in FEB 3 then they need write something else in another date , how can I store these text on different dates ? here is my code :

Code:
/retriveData :

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *saving = [defaults objectForKey:@"saving"];
    saveTextToday.text = saving;


- (void) saveNotes{


        NSString *retriveData = [saveTextToday text];
        NSUserDefaults *defaul = [NSUserDefaults standardUserDefaults];
        [defaul setObject:retriveData forKey:@"saving"];

        [defaul synchronize];


}
This post is nearly identical to your previous post, where you asked about saving to a plist "on a date". You didn't explain yourself well in that post, either.

If you want to save and load notes based on the current date, you need to create code that turns today's date into a string that you could use as a key for NSUserDefaults. Here is a method like that:


Code:
- (NSString*) dateStringForDate: (NSDate *) theDate;
{
  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  //Create a date in form YYYY|MM|DD, e..g 2011|12|31
  formatter.dateFormat = @"y|MM|dd";
  [formatter release];
  return [formatter stringFromDate: theDate];
}
To use it, you would replace the key "saving" with a date string for the date where you want to save your notes:



Code:
/retriveData :

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *dateString = [self dateStringForDate: [NSDate date]];
    NSString *saving = [defaults objectForKey: dateString];
    saveTextToday.text = saving;


- (void) saveNotes{


        NSString *retriveData = [saveTextToday text];
        NSUserDefaults *defaul = [NSUserDefaults standardUserDefaults];
        NSString *dateString = [self dateStringForDate: [NSDate date]];
        [defaul setObject:retriveData forKey: dateString];

        [defaul synchronize];


}
Is that what you're trying to do?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 07:33 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 38
iOS.Lover is on a distinguished road
Default

I think my question is clear ! but I modify it again .

Assume my calendar app has some part , one shows date , one for storing text , and another is button which forward and backward date and time .

user needs save some note for today or tomorrow or another day , actually it's a kind of reminder . What I try to do is that create something which users can save their notes according to to the specific date ...

for example I write something for tomorrow and this text should be saved in 4 Feb or whatever date and the text should be shown in 4Feb not 5 2 6 13 Feb .

I have created a custom class which shows dates and time

myCustomClass.m:

Code:
- (NSString *) showFullDate {
    
        
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  
      
    
    //ADDING DAY
    offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.day = _dayNumber;
    
    NSDate *nextDate = [calendar dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];
    
    [dateFormatter setDateFormat:@"EEEE ، d MMMM"];
	NSString *currDay = [dateFormatter stringFromDate:nextDate];
    
    [NSString stringWithFormat:@"%@",currDay];
    
    [calendar release];
    [dateFormatter release];
    return currDay;
    
}

and then if I would move dates to forward days I use this method :

Code:
- (void)nextDay {

    [self showFullDate];
   
    _dayNumber ++;   
}
in my view controller I use this method
myLable.text = [customClass showFullDate];

so the saving date should be equal to the showFullDate .

Last edited by iOS.Lover; 02-03-2012 at 08:09 AM.
iOS.Lover is offline   Reply With Quote
Old 02-03-2012, 08:20 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iOS.Lover View Post
I think my question is clear ! but I modify it again .

Assume my calendar app has some part , one shows date , one for storing text , and another is button which forward and backward date and time .

user needs save some note for today or tomorrow or another day , actually it's a kind of reminder . What I try to do is that create something which users can save their notes according to to the specific date ...

for example I write something for tomorrow and this text should be saved in 4 Feb or whatever date and the text should be shown in 4Feb not 5 2 6 13 Feb .

I have created a custom class which shows dates and time

myCustomClass.m:

Code:
- (NSString *) showFullDate {
    
        
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  
      
    
    //ADDING DAY
    offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.day = _dayNumber;
    
    NSDate *nextDate = [calendar dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];
    
    [dateFormatter setDateFormat:@"EEEE ، d MMMM"];
	NSString *currDay = [dateFormatter stringFromDate:nextDate];
    
    [NSString stringWithFormat:@"%@",currDay];
    
    [calendar release];
    [dateFormatter release];
    return currDay;
    
}

and then if I would move dates to forward days I use this method :

Code:
- (void)nextDay {

    [self showFullDate];
   
    _dayNumber ++;   
}
in my view controller I use this method
myLable.text = [customClass showFullDate];

so the saving date should be equal to the showFullDate .

I posted code that would let you save your notes to a unique key in user defaults based on the date you passed in. Use that.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 08:56 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 38
iOS.Lover is on a distinguished road
Default

Thank you , your codes works fine but the problem is in retrieving data which I am working on it

Last edited by iOS.Lover; 02-03-2012 at 09:02 AM.
iOS.Lover is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
20 members and 372 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, imac74, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, QuantumDoja, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0