0

簡単な錠剤のリマインダー iOS アプリケーションを作成したいと思います (通知はひとまず脇に置いておきます)。繰り返しのある錠剤の単一の DB レコードを作成し、特定の日が繰り返しによって「生成された」日付と交差するかどうかを確認したいと考えています。

例: 4 月 12 日に開始して 4 月 20 日に終了する錠剤の服用期間を、2 日おきに午後 3 時に設定します。したがって、この丸薬はこの日付に有効です:

  • 4月12日午後3時
  • 4月14日午後3時
  • 4月16日午後3時
  • 4月18日午後3時
  • 4月20日午後3時

質問:

  • 「2 日ごと」という情報を説明できるデータの種類はどれですか? NSDateInterval は良い解決策でしょうか?

  • 特定の日が繰り返しスキームと一致することを確認するにはどうすればよいですか? (つまり、前の例で 4 月 13 日が有効な日付であるかどうかを確認し、答えとして「いいえ」を取得します)

4

1 に答える 1

2

基準を満たす必要があるクラスの例を次に示します。

main.m

#import <Foundation/Foundation.h>
#import "DateChecker.h"

int main(int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Specify a period in days
    NSInteger period = 2;

    // Set the start/end/current days.  These can be retrieved from a database or elsewhere
    NSDate *startDate = [NSDate dateWithString:@"2012-04-12 00:00:00 +0000"];
    NSDate *endDate = [NSDate dateWithString:@"2012-04-20 00:00:00 +0000"];
    NSDate *currentDate = [NSDate dateWithString:@"2012-04-14 00:00:00 +0000"];

    // Actually do the checking.  This could alternatively be offloaded to a function in DateChecker
    if([DateChecker date:currentDate isBetweenDate:startDate andDate:endDate])
    {
        if([DateChecker date:currentDate fallsOnStartDate:startDate withInterval:period])
        {
            NSLog(@"The date %@ falls in the specified date period & is on the interval date.", currentDate);
        }
        else
        {
            NSLog(@"The date %@ falls in the specified date period & is NOT on the interval date.", currentDate);
        }
    }
    else
    {
        NSLog(@"The date %@ does not fall in the specified date period.", currentDate);
    }

    [pool release];

    return 0;
}

DateChecker.h

#import <Foundation/Foundation.h>

@interface DateChecker : NSObject
{
}
+ (BOOL)date:(NSDate*)date fallsOnStartDate:(NSDate*)beginDate withInterval:(NSInteger)daysInterval;
+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate;

@end

DateChecker.m

#import "DateChecker.h"

@implementation DateChecker

+ (BOOL)date:(NSDate*)date fallsOnStartDate:(NSDate*)beginDate withInterval:(NSInteger)daysInterval
{
    NSDateComponents *components;
    NSInteger days;

    // Get the number of days since the start date
    components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit fromDate: beginDate toDate: date options: 0];
    days = [components day];

    // If the number of days passed falls on the interval, then retrun YES
    if(days % daysInterval == 0)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;

    if ([date compare:endDate] == NSOrderedDescending) 
        return NO;

    return YES;
}

@end

これで、要件を処理できるクラスを作成する方法がわかります。これをコマンドラインアプリとして実行しましたが、問題なく動作しているようです。関数

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate

NSDateが他の2つのNSDateの間に発生するかどうかを確認する方法から丁寧に取得されました。

于 2012-05-21T08:15:15.617 に答える