2012-05-19 4 views
0

간단한 알리미 알림 iOS 응용 프로그램을 만들고 싶습니다 (잠시 알림을 남겨 둡니다). 반복을 가지고있는 피임약에 대해 하나의 DB 레코드를 만들고 반복으로 특정 날짜와 "생성 된"날짜가 교차하는지 확인하고 싶습니다.날짜가 특정 반복의 날짜 범위에 있는지 확인하는 방법은 무엇입니까?

예 :나는 4 월 12 일에 시작하여 4 월 20 일에 끝내고 2 일 간격으로 오후 3시에 반복하는 약 기간을 정했다.

  • 년 4 월 12 오후 3시
  • 4 월 (14) 오후 3시
  • 4 월 (16) 오후 3시
  • 4 월 (18) 오후 3시에서
  • 월 20 일 :이 날짜에 따라서이 약을 유효 오후 3시에서

질문 :

  • "2 일마다"정보를 설명 할 수있는 데이터 유형은 무엇입니까? NSDateInterval 좋은 해결책이 될 것이라고?

  • 특정 날짜가 내 반복 계획과 일치하는지 어떻게 확인할 수 있습니까?

    main.m

    : 여기

답변

2

이 기준을 충족해야한다 예를 들어 클래스입니다 (4 월 (13)는 앞의 예에 대한 유효한 날짜 인 경우, 즉 확인하고 "NO"답변으로 얻을)
#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 

How to Check if an NSDate occurs between two other NSDates에서 친절하게 얻은 것입니다.

관련 문제