title: NSPredicate: Unable To Parse String
body: <p>Im doing predicate to fetch all transaction within specific date range based on transaction type.</p>
  
  <p>Here is the predicate:</p>
  
  <pre><code>NSString *predicate = [NSString stringWithFormat:@"(date &gt;= %@ AND date &lt; %@) AND (%@)", from, to, defaultType];
  </code></pre>
  
  <p>Here is the complete code:</p>
  
  <pre><code>- (NSArray *)transactionFromMonth:(NSInteger)fromMonth toMonth:(NSInteger)toMonth ofTypes:(NSArray *)transactionTypes
  {
     // get the current calendar of the device
     NSCalendar *calendar = [NSCalendar currentCalendar];
  
     // create date components using current calendar
     NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay
                                  | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
  
     // get year
     NSInteger currentYear = [components year];
  
     // create fromDate (The first day of the month) using the current year, passing month, and 1 as the day
     // to form the starting day of the month
     // e.g when toMonth has a value of 1, then the resulting
     // date will be January 1, (current year of the device's calendar)
     NSDateComponents *comps = [[NSDateComponents alloc] init];
     [comps setDay:1];
     [comps setMonth:fromMonth];
     [comps setYear:currentYear];
     NSDate *from = [[NSCalendar currentCalendar] dateFromComponents:comps];
  
  
     // create toDate
     //
     // Set your month here
     [comps setMonth:toMonth];
  
     // get the number of days of "to" month
     NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit
                                 inUnit:NSMonthCalendarUnit
                                forDate:[calendar dateFromComponents:comps]];
  
     [comps setDay:range.length+1];
  
     NSDate *to = [[NSCalendar currentCalendar] dateFromComponents:comps];
  
     NSLog(@"to Months %@", to);
     NSString *defaultType = [NSString stringWithFormat:@"type = %d", [[transactionTypes objectAtIndex:0] integerValue]];
  
     NSMutableString *types = [[NSMutableString alloc] initWithString: defaultType ];
     for  ( int i = 1; i &lt; transactionTypes.count; i++ )
     {
        [defaultType stringByAppendingFormat:@" OR type = %d", [[transactionTypes objectAtIndex:i] integerValue] ];
     }
  
     // here is the predicate causing the error
     // 
     //
     NSString *predicate = [NSString stringWithFormat:@"(date &gt;= %@ AND date &lt; %@) AND (%@)", from, to, defaultType];
     return [self.transaction filteredSetUsingPredicate: [NSPredicate predicateWithFormat:predicate]].allObjects;
  }
  </code></pre>
  
  <p>Here is the declaration of transaction model:
  Note:  .h and .m of the below model is autogenerated.</p>
  
  <p><img src="https://i.stack.imgur.com/FEQOH.png" alt="enter image description here"></p>
  
  <p>Here is the sample call:</p>
  
  <pre><code>// I want to retrieve all transaction from January 1 to January 31 in device's current calendar year of type kTransactionTypeAddFund (which has the value of 0)
  [self transactionFromMonth:1 toMonth:1 ofTypes:@[ @(kTransactionTypeAddFund) ]];
  </code></pre>
  
  <p>Result:</p>
  
  <blockquote>
    <p>reason: 'Unable to parse the format string "(date >= 2014-12-31
    16:00:00 +0000 AND date &lt; 2015-01-31 16:00:00 +0000) AND (type = 0)"'</p>
  </blockquote>