【发布时间】:2011-04-01 02:29:30
【问题描述】:
由于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];
更复杂的是,上面的表示是格林威治标准时间,显然时区是在创建 NSDate 对象期间设置的。我看到的上述代码的替代方法是获取 GMT 偏移量,在 dateWithTimeInterval:sinceDate: 方法中使用它来获取 GMT 日期,然后使用 NSDateFormatter 写出上述字符串。但是,因为这会产生更多开销。
有什么方法可以只获取那个 xml 字符串吗?
【问题讨论】:
标签: iphone objective-c cocoa plist nsdate