-2

I'm calling a method passing two arrays to it as follow :

NSArray *marked_dates = [NSArray arrayWithObjects:[dateFormat dateFromString:@"19/03/2014"],[dateFormat dateFromString:@"17/04/2014"],[dateFormat dateFromString:@"12/02/2014"], nil];
NSArray *marked_colors = [NSArray arrayWithObjects:[UIColor greenColor],[UIColor greenColor],[UIColor greenColor],nil];
[calendar markDates:marked_dates withColors:marked_colors];

Below is the method receiving this :

-(void)markDates:(NSArray *)dates withColors:(NSArray *)colors {
    self.markedDates = dates;
    self.markedColors = colors;

    NSLog(@"%@",[NSString stringWithFormat:@"%d",[dates count]]);

    for (int i = 0; i<[self.markedDates count]; i++) {
        NSLog(@"%@",[NSString stringWithFormat:@"%@", self.markedDates[i]]);
    }

    [self setNeedsDisplay];
}

The log says 0 and I obviously don't get into the for loop.

Thanks for any help.

EDIT : I will also vote up for any information about the warning : "format string is not a string literal"

Below the declaration of the properties :

@property (nonatomic, retain) NSArray *markedDates;
@property (nonatomic, retain) NSArray *markedColors;

EDIT :

So yes, the formater gives null values. Here is how I did it :

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSLog(@"%@",[NSString stringWithFormat:@"%@",[dateFormat dateFromString:@"19/03/2014"]]);
//this prints "null"

EDIT with solution :

Bad mistake, I misconfigured the formatter using this worked for me, thank you all for your help.

[dateFormat setDateFormat:@"dd/MM/yyyy"];
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • 3
    are both arrays you pass empty? have you once logged the arrays before sending them in the method call? Maybe the date formatter doesn't produce dates but nil... – Volker Mar 11 '14 at 09:50
  • Can you show the declaration of the `markedDates` and `markedColors` properties. – Fogmeister Mar 11 '14 at 09:50
  • 1
    show the code on how you created the formatter `dateFormat`. – GoGreen Mar 11 '14 at 09:52
  • 2
    Probably your `[dateFormat dateFromString:@"19/03/2014"]` is returning nil. So `marked_dates` is nil. – iPP Mar 11 '14 at 09:52
  • 1
    Log marked_dates after it is created and see if it is empty or has some data – Shanti K Mar 11 '14 at 09:56
  • re format string is not a literal see http://stackoverflow.com/questions/1677824/warning-format-not-a-string-literal-and-no-format-arguments ; – Volker Mar 11 '14 at 09:58
  • 1
    You have MM/dd/yyyy, and your input is dd/MM/yyyy format. – Dan Power Mar 11 '14 at 09:59
  • use `NSLog("%@",[NSString stringWithFormat:@"%@",[dateFormat dateFromString:@"19/03/2014"]]);` – GoGreen Mar 11 '14 at 09:59
  • and 1 more thing. It seems that you are converting the `NSString` values into `NSDate`, and reconverting them to `NSString` in `markDates:withColors:`. If it is used just for the sake of printing, then why do the conversion to `NSDate` in the first place? – GoGreen Mar 11 '14 at 10:06
  • @GoGreen it was just for logging yes. – Loïc Mar 11 '14 at 10:07

3 Answers3

2

The dates array is probably empty due to the date formatter only producing nil values. Fix the date formatter so it will correctly generate NSDate objects and it should work:

[dateFormat setDateFormat:@"dd/MM/yyyy"];
Volker
  • 4,640
  • 1
  • 23
  • 31
0
[dateFormat setDateFormat:@"dd/MM/yyyy"]; ?
Joey Clover
  • 756
  • 5
  • 14
0

First of all you should not create the property name and Local instance variable as same name. Because property it's self create instance var with _Property name.And the issue is with line.

NSArray *marked_dates = [NSArray arrayWithObjects:[dateFormat dateFromString:@"19/03/2014"],[dateFormat dateFromString:@"17/04/2014"],[dateFormat dateFromString:@"12/02/2014"], nil];

Add the date format also. check this again. let me know if you still facing the same issue.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75