【问题标题】:How do I get the NSDate plist representation programmatically?如何以编程方式获取 NSDate plist 表示?
【发布时间】: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


    【解决方案1】:

    如果您真的要创建仅包含一个日期的属性列表,那么您发布的代码似乎是正确的方法。我知道您可能会觉得这有点麻烦,但您始终可以将其粘贴到 NSDate 上的类别中的方法中,以便您可以直接从属性列表格式读取或写入日期。

    【讨论】:

    • 您实际上是对的,但主要问题是dataFromPropertyList:format:errorDescription: 是一种计划弃用和替换的方法,dataWithPropertyList:format:options:error: 仅在 iOS 4.0 及更高版本中可用。我需要我的代码在 3.1.3 及以上系统上运行。在这种情况下,NSDateFormatter 是最好的选择(见下面我的回答)。
    • 那么为什么不测试-dataWithPropertyList:format:options:error: 的存在,如果不可用则回退到-dataFromPropertyList:format:errorDescription:
    【解决方案2】:

    NSDate 的 description 方法返回一个字符串,格式为 YYYY-MM-DD HH:MM:SS ±HHMM。

    编辑:executor21 指出以下部分仅适用于 OS X,不适用于 iOS。

    如果您不想要这种格式,您可以改用descriptionWithCalendarFormat:timeZone:locale: 并定义您自己的格式。见http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSDate/description

    【讨论】:

    • descriptionWithCalendarFormat:timeZone:locale: 是 Mac OS X 的方法,iOS 上不存在。
    • 抱歉,我以为 Apple 会为这两者提供。我将编辑我的帖子以显示这一点。
    • 它也“计划弃用”,所以你不应该在任何一个平台上使用它。
    【解决方案3】:

    在这种情况下,NSDateFormatter 似乎实际上是最好的选择——我需要在 iOS 3.1.3 及更高版本的设备上运行它,所以 dataWithPropertyList:format:options:error: 方法(在 4.0 中引入)不是一个选项,而 dataFromPropertyList:format:errorDescription: 计划弃用。

    另外,我在上面犯了一个错误:应该是

    NSString *xmlRepresentationOfCurrentDate = [[[[[[[NSString alloc] initWithData:[NSPropertyListSerialization dataWithPropertyList:self format:kCFPropertyListXMLFormat_v1_0 options:0 error:NULL] encoding:NSUTF8StringEncoding] autorelease] componentsSeparatedByString:@"<date>"] objectAtIndex:1] componentsSeparatedByString:@"</date>"] objectAtIndex:0];
    

    但实际的全版本解决方案是NSDate 上的类别方法:

    -(NSString *)xmlRepresentation{
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setTimeStyle:NSDateFormatterFullStyle];
    
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
        [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    
        return [[formatter stringFromDate:self] stringByAppendingString:@"Z"];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2014-05-24
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多