Search code examples
iphoneobjective-ciosxcode4

Unable to Fetch Date from Array of Dates Using NSPredicate


i am fetching array of dates which come between two date.. and i am getting error when executing this code don't know where i went wrong :(

my code is

  NSArray *Dates = [NSArray arrayWithObjects:@"02-02-2012", @"04-02-2012", @"04-02-2012",      @"03-02-2012",@"04-02-2010", @"04-02-2012", @"04-02-2011", @"09-02-2012", @"12-02-2012 ",      @"11-02-2012", nil];
  NSString *startStringDate= @"03-02-2012";
  NSString *endStringDate = @"09-02-2012";

  //converting NSString to NSDate
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"dd-MM-yyyy"];
  NSDate *StartDate = [dateFormatter dateFromString:startStringDate];
  NSDate *endDate = [dateFormatter dateFromString:endStringDate];

 // output of StartDate is 03-02-2012 08:37:48 +0000
 // output of endDate is 09-02-2012 08:37:48 +0000

 //using filteredArrayUsingPredicate to fetch date which comes between StartDate and  endDate

  NSPredicate *findS = [NSPredicate predicateWithFormat:@"(SELF > %@) AND (SELF < %@)", StartDate, endDate];
  NSArray *result = [Dates filteredArrayUsingPredicate:findS];

when i run this code i got error as: [__NSDate length]: unrecognized selector sent to instance 0x982d5a0'

can somebody help me in solving this Thanks in Advance


Solution

  • NSArray *Dates = [NSArray arrayWithObjects:[dateFormatter dateFromString:@"02-02-2012"],[dateFormatter dateFromString:@"04-02-2012"],nil];
    

    create your array like this for date than try it may be work...