[英]Convert NSDate to Unix Time Stamp for use in Dictionary
While there are numerous examples on SO and the web of converting the current date to a unix timestamp, I can't seem to find one for any date. 尽管在SO和网络上有很多将当前日期转换为unix时间戳的示例,但我似乎找不到任何日期的日期。
This code produces error shown. 此代码产生显示的错误。
NSDate *date = self.datepicker.date;
time_t start = (time_t) [date timeIntervalSince1970];//collection element of time_t is not an objective-c object
Would appreciate any suggestions. 将不胜感激任何建议。
I believe that code will work and the error message relates to a different statement. 我相信该代码将起作用,并且错误消息与另一条语句有关。
If you want to put that time_t
into a dictionary then you'll need to wrap it in an NSNumber
object. 如果要将time_t
放入字典中,则需要将其包装在NSNumber
对象中。 However you may as well keep it as an NSTimeInterval
( double
) and cast it when using it: 但是,您也可以将其保留为NSTimeInterval
( double
)并在使用时将其NSTimeInterval
:
NSDate *date = self.datepicker.date;
NSTimeInterval start = [date timeIntervalSince1970]
dict[@"startTime"] = @(start);
However you can also cast it before adding it to the dictionary if you really want to. 但是,如果您确实愿意,也可以在将其添加到字典之前进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.