简体   繁体   English

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

[英]NSArray contains nothing after method call

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 : 这是我的做法:

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

The dates array is probably empty due to the date formatter only producing nil values. 由于日期格式化程序仅产生nil值,所以dates数组可能为空。 Fix the date formatter so it will correctly generate NSDate objects and it should work: 修复日期格式化程序,以便它可以正确生成NSDate对象并且可以正常工作:

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

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. 因为属性是它自己创建的具有_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. 让我知道您是否仍然面临同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM