Objective c NSDateFormatter stringFromDate返回空字符串
下面是我如何设置它的:Objective c NSDateFormatter stringFromDate返回空字符串,objective-c,Objective C,下面是我如何设置它的: self.df = [[NSDateFormatter alloc] init]; [self.df setFormatterBehavior:NSDateFormatterBehavior10_4]; [self.df setDateFormat:@"posted: MM-dd-YYYY"]; 以下是我在跟踪中对它的称呼: NSLog(@"%@, %@, %@, %@", df, post.date, [post.date class], [df stringFro
self.df = [[NSDateFormatter alloc] init];
[self.df setFormatterBehavior:NSDateFormatterBehavior10_4];
[self.df setDateFormat:@"posted: MM-dd-YYYY"];
以下是我在跟踪中对它的称呼:
NSLog(@"%@, %@, %@, %@", df, post.date, [post.date class], [df stringFromDate:post.date]);
以下是输出:
<NSDateFormatter: 0x8440d60>, 2012-02-09 12:40:38 +0000, __NSDate,
,2012-02-09 12:40:38+0000,
它不是空的,它是“”
具有date属性的post类由core data自动生成。我看不出这有什么不同,但这是唯一一件看起来有点不寻常的事情。你不能像那样使用setDateFormat 我尝试了这个代码,它成功了:
[self.df setDateFormat:@"MM-dd-YYYY"];
现在,您可以执行以下操作:
NSString *myString = [NSString stringWithFormat:@"posted:%@", [df stringFromDate:post.date]];
不能像那样使用setDateFormat 我尝试了这个代码,它成功了:
[self.df setDateFormat:@"MM-dd-YYYY"];
现在,您可以执行以下操作:
NSString *myString = [NSString stringWithFormat:@"posted:%@", [df stringFromDate:post.date]];
也为post.date打印一份。也为post.date打印一份。谢谢,@tonio.mg,这确实是格式字符串中游离的“post:”。事实证明,您可以在格式字符串中使用单引号转义文本,因此这也可以起作用:[self.df setDateFormat:@“'posted:'MM dd YYYY];谢谢,@tonio.mg,这确实是格式字符串中的流浪“posted:”。事实证明,您可以在格式字符串中使用单引号转义文本,因此这也可以起作用:[self.df setDateFormat:@“'posted:'MM dd YYYY];