[英]Assigning [NSDate date] to NSString in Swift
In Objective C, we could easily assign present date to NSString like string = [NSDate date];
在Objective C中,我们可以轻松地将当前日期分配给NSString,如string = [NSDate date];
. 。
Could someone help me how to assign the same in Swift ? 有人可以帮助我如何在Swift中分配相同的内容吗? Thanks in advance. 提前致谢。
First off, your statement... 首先,你的陈述......
In Objective C, we could easily assign present date to NSString like string = [NSDate date]; 在Objective C中,我们可以轻松地将当前日期分配给NSString,如string = [NSDate date];
Is complete rubbish. 完全是垃圾。
In Objective-C you need to use an NSDateFormatter
to render an NSString
out of an NSDate
. 在Objective-C中,您需要使用NSDateFormatter
从NSDate
呈现NSString
。
You would do it something like this... 你会这样做的......
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle
NSString *string = [df stringFromDate:date];
Now, in Swift, not surprisingly, it is EXACTLY the same. 现在,在Swift中,毫不奇怪,它完全相同。
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
let string = dateFormatter.stringFromDate(date)
也许你想要这个:
let string = NSDate().description
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.