方法调用后NSArray不包含任何内容

时间:2021-09-10 21:32:04

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.

日志显示为0,我显然没有进入for循环。

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 :

所以是的,formater给出了null值。我是这样做的:

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"];

3 个解决方案

#1


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:

由于日期格式化程序仅生成nil值,因此dates数组可能为空。修复日期格式化程序,以便它将正确生成NSDate对象,它应该工作:

[dateFormat setDateFormat:@"dd/MM/yyyy"];

#2


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.

首先,您不应将属性名称和Local实例变量创建为同名。因为属性是自己创建带有_Property名称的实例var。问题在于行。

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.

同时添加日期格式。再看一遍。如果你还面临同样的问题,请告诉我。

#3


0  

[dateFormat setDateFormat:@"dd/MM/yyyy"]; ?

#1


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:

由于日期格式化程序仅生成nil值,因此dates数组可能为空。修复日期格式化程序,以便它将正确生成NSDate对象,它应该工作:

[dateFormat setDateFormat:@"dd/MM/yyyy"];

#2


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.

首先,您不应将属性名称和Local实例变量创建为同名。因为属性是自己创建带有_Property名称的实例var。问题在于行。

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.

同时添加日期格式。再看一遍。如果你还面临同样的问题,请告诉我。

#3


0  

[dateFormat setDateFormat:@"dd/MM/yyyy"]; ?