【问题标题】:Error in dateFormat returns nildateFormat 中的错误返回 nil
【发布时间】:2016-08-13 17:32:53
【问题描述】:

作为练习 Objective-C 的练习,我将 Swift 代码转换为 Objective-C。在转换了 1.5k 行之后,我发现自己在过去的一期中被屏蔽了几天。

在我的 TableViewController 的一些单元格中,描述标签根本没有出现。我检查了我的代码,似乎传递给单元格的字符串实际上是空的或为零。

在转换将日期返回到我的单元格的 Swift 代码时,我似乎犯了语法错误。我犯了什么错误导致日期为零?

代码:

ViewController.m

...

NSDate const *now = [[NSDate alloc] init];
NSDate *date = [[NSDate alloc] init];

self.sections =  [[NSMutableArray<NSString *> alloc]init];
self.items = [[NSMutableArray<NSMutableArray<TableItem *> *> alloc]init];
self.sectionItems = [[NSMutableArray<TableItem *> alloc]init];

...

这是未正确加载的单元格数据(空字符串传递给theDescription):

anItem = [[TableItem alloc ]initWithTitle: @"Custom: dd MMM yyyy HH:mm:ss" theDescription: [NSString stringWithFormat:@"%@", [now toString: [DateFormat CustomDateFormat:@"dd MMM yyyy HH:mm:ss"]]]];
        [_sectionItems addObject: anItem];

//theDescription should be: @"25 April 2016 15:04:57" after this ^, but is actually nil or @""

anItem = [[TableItem alloc ]initWithTitle: @"ISO8601(Year)" theDescription: [NSString stringWithFormat:@"%@", [now toString: [DateFormat ISODateFormat: ISOFormatYear]]]];
        [_sectionItems addObject: anItem];

//theDescription should be: @"2016" after this ^, but is actually nil or @""

这就是我认为我犯了错误的地方:

原始 Swift 语法:

扩展.swift

toString() 函数

 case .ISO8601(let isoFormat):
        dateFormat = (isoFormat != nil) ? isoFormat!.rawValue : ISO8601Format.DateTimeMilliSec.rawValue
        zone = NSTimeZone.localTimeZone()

我尝试了什么:

扩展名.m

else if([format.dateFormatType compare: ISO8601DateFormatType] == NSOrderedSame) {
    NSString *isoFormat = ISO8601DateFormatType;
    dateFormat =  (isoFormat != nil) ? isoFormat : ISOFormatDateTimeMilliSec;
    zone = [NSTimeZone localTimeZone];
}

【问题讨论】:

  • 我没有查看您的整个代码,但我怀疑您的描述标签存在但带有空字符串。检查您设置为描述标签的字符串不为空或为空。
  • 你是对的:它们是空的。我需要弄清楚为什么会这样。因此我的问题在这里。我一直在查看它,但似乎找不到问题所在。
  • 这是什么now 变量?
  • 它可能很简单,例如在创建数据并将其传递给它们之前创建的单元格。我在 obj-c 方面不够流利,无法从您的代码中看出这一点,但这是我在涉及 tableViews 时首先检查的事情之一。
  • 哇,这是很多代码。您能否减少您的问题或附上一个提供Minimal, Complete, and Verifiable example 问题的示例项目?也许将您的 toString 代码抽象到一个新项目中,并提供有关预期内容、实际输出内容以及它们有何不同的测试用例。

标签: ios objective-c swift uitableview nsdate


【解决方案1】:

问题出在这一行:

NSDateFormatter *formatter = [NSDate formatter : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];

dateFormat@"Custom",因为它是在这里设置的:

else if([format.dateFormatType compare: CustomDateFormatType] == NSOrderedSame) {
    NSString *string = CustomDateFormatType;
    dateFormat = string;
}

这将在此处传递给您的 NSDateFormatter 构造函数:

formatter.dateFormat = format;

@"Custom" 不是有效的日期格式字符串。而是传入format.formatDetails

NSDateFormatter *formatter = [NSDate formatter : format.formatDetails : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];

我应该注意到,在 Objective-C 中不使用命名参数会使您的代码更难阅读。

【讨论】:

  • 至于命名参数,我马上开始将所有func参数转换为命名参数!
  • 我也赞成你的回答!我不能说够了,谢谢!
  • 别担心,乐于助人!别忘了回来奖励赏金。编码愉快!
猜你喜欢
  • 1970-01-01
  • 2016-10-05
  • 2011-04-07
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
相关资源
最近更新 更多