如何以编程方式获取 NSDate plist 表示形式?
由于plist是xml,即文本,因此当将NSDate对象写入plist时,结果是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<date>2011-04-01T02:09:15Z</date>
</plist>
我想获取该字符串(011-04-01T02:09:15Z
)直接,没有所有周围环境。必须有一种更明智的方法来做到这一点:
NSString *xmlRepresentationOfCurrentDate = [[[[[[[NSString alloc] initWithData:[NSPropertyListSerialization dataFromPropertyList:self format:kCFPropertyListXMLFormat_v1_0 options:0 error:NULL] encoding:NSUTF8StringEncoding] autorelease] componentsSeparatedByString:@"<date>"] objectAtIndex:1] componentsSeparatedByString:@"</date>"] objectAtIndex:0];
使事情变得复杂的是上面的表示是 GMT,并且显然时区是在创建 NSDate 对象期间设置的。我看到的上述代码的替代方法是获取 GMT 偏移量,在 dateWithTimeInterval:sinceDate: 方法中使用它来获取 GMT 日期,然后使用 NSDateFormatter 来获取 GMT 日期。写出上面的字符串。然而,因为这会带来更多的开销。
有什么办法可以只获取该 xml 字符串吗?
Since plist is xml, which is text, when an NSDate object is written into a plist, the result is something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<date>2011-04-01T02:09:15Z</date>
</plist>
I would like to obtain that string (011-04-01T02:09:15Z
) directly, without all the surroundings. There has to be a more sensible way to do it than:
NSString *xmlRepresentationOfCurrentDate = [[[[[[[NSString alloc] initWithData:[NSPropertyListSerialization dataFromPropertyList:self format:kCFPropertyListXMLFormat_v1_0 options:0 error:NULL] encoding:NSUTF8StringEncoding] autorelease] componentsSeparatedByString:@"<date>"] objectAtIndex:1] componentsSeparatedByString:@"</date>"] objectAtIndex:0];
What complicates it is the fact that the representation above is GMT, and apparently the timezone is set during the creation of the NSDate object. An alternative to the above code I see is to get the GMT offset, use it in the dateWithTimeInterval:sinceDate:
method to get the GMT date, and then use the NSDateFormatter
to write out the above string. However, since that has even more overhead.
Is there any way to get just that xml string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实要创建仅包含单个日期的属性列表,那么您发布的代码似乎是正确的方法。我可以理解您可能会觉得这有点麻烦,但您始终可以将其粘贴到 NSDate 上的类别中的方法中,以便您可以直接从属性列表格式读取或写入日期。
If you're really creating property lists that contain just a single date, the code you posted seems like the right way to go. I can see how you might feel that's a little cumbersome, but you could always paste that into a method in a category on NSDate so that you can read or write dates directly from or to property list format.
NSDate 的
description
方法返回一个使用 YYYY-MM-DD HH:MM:SS ±HHMM 格式的字符串。编辑:executor21指出以下部分仅适用于OS X,不适用于iOS。
如果您不想要该格式,可以使用
descriptionWithCalendarFormat:timeZone:locale:
代替并定义您自己的格式。请参阅NSDate's
description
method returns a string using the format YYYY-MM-DD HH:MM:SS ±HHMM.Edit: executor21 pointed out that the section below is only valid for OS X, not iOS.
If you don't want that format, you can use
descriptionWithCalendarFormat:timeZone:locale:
instead and define your own format. See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSDate/description考虑到这种情况,NSDateFormatter 实际上是最好的选择——我需要在 iOS 3.1.3 及更高版本的设备上运行它,所以
dataWithPropertyList:format:options:error:
方法(在4.0) 不是一个选项,而dataFromPropertyList:format:errorDescription:
预计将被弃用。另外,我在上面犯了一个错误:它应该是
但实际的全版本解决方案是
NSDate
上的类别方法:It seems like the NSDateFormatter is actually the best option given the circumstances -- I need to run this on devices with iOS 3.1.3 and above, so
dataWithPropertyList:format:options:error:
method (introduced in 4.0) isn't an option, whiledataFromPropertyList:format:errorDescription:
is slated for deprecation.Also, I made a mistake above: it should be
But the actual, all-versions solution is a category method on
NSDate
: