Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3800714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:56:10+00:00 2026-05-19T13:56:10+00:00

I need to quickly produce some functions which will allow me to store and

  • 0

I need to quickly produce some functions which will allow me to store and retrieve some date ranges. Then evaluate the dates based on todays date, then add them to an array.

I’m not exactly sure how many ranges I’ll need to keep.

Perhaps you’ve seen a similar example of this I can use for reference?

EDIT.
I need to save them for use when the app is next run too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:56:11+00:00Added an answer on May 19, 2026 at 1:56 pm

    You can represent a date range in two ways:

    • start date + end date
    • start date + duration

    You can then represent a date as an NSDate instance, as a double (or, if you desperately need to save memory, as an int, like UNIX timestamp). I’d make a class for that.

    @interface DateRange : NSObject <NSCoding>
    {
        NSTimeInterval start, end;
    }
    @property (nonatomic, assign) NSDate* startDate;
    @property (nonatomic, assign) NSDate* endDate;
    @property (nonatomic, assign) NSTimeInterval duration;
    - (BOOL) containsDate: (NSDate *) date;
    @end
    
    @implementation DateRange
    
    - (NSDate *) startDate
    {
        return [NSDate dateWithTimeIntervalSinceReferenceDate: start];
    }
    
    - (void) setStartDate: (NSDate *) date
    {
        start = [date timeIntervalSinceReferenceDate];
    }
    
    - (NSDate *) endDate
    {
        return [NSDate dateWithTimeIntervalSinceReferenceDate: end];
    }
    
    - (void) setEndDate: (NSDate *) date
    {
        end = [date timeIntervalSinceReferenceDate];
    }
    
    - (NSTimeInterval) duration
    {
        return end - start;
    }
    
    - (void) setDuration: (NSTimeInterval) newDuration
    {
        end = start + newDuration;
    }
    
    - (BOOL) containsDate: (NSDate *) date
    {
        NSTimeInterval d = [date timeIntervalSinceReferenceDate];
        return d > start && d < end;
    }
    
    - (id) initWithCoder: (NSCoder *) coder
    {
        if ( ( self = [super init] ) ) {
            start = [coder decodeDoubleForKey: @"start"];
            end = [coder decodeDoubleForKey: @"end"];
        }
        return self;
    }
    
    - (void) encodeWithCoder: (NSCoder *) coder
    {
        [coder encodeDouble: start forKey: @"start"];
        [coder encodeDouble: end forKey: @"end"];
    }
    
    - (BOOL) isEqual: (id) dateRange
    {
        if ( [self class] != [dateRange class] )
            return NO;
        return [self.startDate isEqualToDate: ((DateRange *)dateRange).startDate] && [self.endDate isEqualToDate: ((DateRange *)dateRange).endDate];
    }
    
    - (NSInteger) hash
    {
        return (NSInteger) (end - start);
    }
    
    @end
    

    The bonus point is that you can store as many ranges in Cocoa collections (such as NSArray) as needed. I leave a better implementation of -isEqual: and -hash as an exercise to you, dear reader.

    To save a DateRange object in NSUserDefaults:

    NSData *dateRangeArchive = [NSKeyedArchiver archivedDataWithRootObject: dateRange];
    [[NSUserDefaults standardUserDefaults] setObject: dateRangeArchive forKey: myKey];
    

    To read a DateRange object from NSUserDefaults:

    DateRange *dr = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] dataForKey: myKey]]; // returns an autoreleased DateRange object
    

    Similarly, if you need to store an NSArray of DateRanges, you can either archive the whole array or create an array of archived DateRanges (the former should be faster):

    NSData *dateRangeArchive = [NSKeyedArchiver archivedDataWithRootObject: arrayOfDateRanges];
    [[NSUserDefaults standardUserDefaults] setObject: dateRangeArchive forKey: myKey];
    

    If you think that’s too much repetitive code for your project, you can extend NSUserDefaults like this:

    @interface NSUserDefaults (MyExtensions)
    - (DateRange *) dateRangeForKey: (NSString *) defaultName;
    - (void) setDateRange: (DateRange *) dateRange forKey: (NSString *) defaultName;
    @end
    
    @implementation NSUserDefaults (MyExtensions)
    
    - (DateRange *) dateRangeForKey: (NSString *) defaultName
    {
        return [NSKeyedUnarchiver unarchiveObjectWithData: [self dataForKey: defaultName]];
    }
    
    - (void) setDateRange: (DateRange *) dateRange forKey: (NSString *) defaultName
    {
        [self setObject: [NSKeyedArchiver archivedDataWithRootObject: dateRange] forKey: defaultName];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compress some binary files as quickly as possible with PHP -
Need some regular expressions help. So far I have my code working to allow
I need some help to understand analysing recursive algorithms. I quickly made this algorithm
I have the need to relatively quickly be able to determine if a set
I need to get up and running quickly with a MT TCP server implemented
I need to get up to speed with C++ quite quickly (I've never used
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
I am looking for some advice on how to allow easy customisation and extension
I need to add a third party component to one of our products (which
I need to quickly save a re-ordered sequence back to my items' integer sortOrder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.