【发布时间】: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