dateFormat 中的错误返回 nil [英] Error in dateFormat returns nil
问题描述
作为练习 Objective-C 的练习,我将 Swift 代码转换为 Objective-C.转换了 1.5k 行后,我发现自己在过去的几天里被最后一期屏蔽了.
As an exercise to practice Objective-C, I am converting Swift code to Objective-C. After 1.5k lines converted, I find myself blocked for the past few days on one last issue.
在我的 TableViewController 的几个单元格中,描述标签根本没有出现.我检查了我的代码,似乎传递给单元格的字符串实际上是空的或 nil.
In a few cells of my TableViewController, the description labels don't appear at all. I checked my code, and it seems the strings that are passed to the cells are actually empty or nil.
我似乎在转换将日期返回到我的单元格的 Swift 代码时犯了语法错误.我犯了什么错误导致日期为零?
I seem to have made a syntax error in converting the Swift code that returns the date to my cells. What error have I made that makes the date nil?
...
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
):
This is the cell data that doesn't load correctly (empty strings are passed to 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 @""
这里是我认为我犯了错误的地方:
Here's where I think I've made a mistake:
原始 Swift 语法:
Original Swift syntax:
toString() 函数
toString() function
case .ISO8601(let isoFormat):
dateFormat = (isoFormat != nil) ? isoFormat!.rawValue : ISO8601Format.DateTimeMilliSec.rawValue
zone = NSTimeZone.localTimeZone()
我尝试了什么:
else if([format.dateFormatType compare: ISO8601DateFormatType] == NSOrderedSame) {
NSString *isoFormat = ISO8601DateFormatType;
dateFormat = (isoFormat != nil) ? isoFormat : ISOFormatDateTimeMilliSec;
zone = [NSTimeZone localTimeZone];
}
推荐答案
问题出在这一行:
NSDateFormatter *formatter = [NSDate formatter : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];
dateFormat
是 @"Custom"
,因为它在这里设置:
dateFormat
is @"Custom"
, as it was set here:
else if([format.dateFormatType compare: CustomDateFormatType] == NSOrderedSame) {
NSString *string = CustomDateFormatType;
dateFormat = string;
}
这被传递到您的 NSDateFormatter
构造函数中:
This is passed into your NSDateFormatter
constructor here:
formatter.dateFormat = format;
@"Custom"
不是有效的日期格式字符串.相反,传入 format.formatDetails
:
@"Custom"
is not a valid date format string. Instead, pass in format.formatDetails
:
NSDateFormatter *formatter = [NSDate formatter : format.formatDetails : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];
我应该注意,在 Objective-C 中不使用命名参数会使您的代码更难阅读.
I should note that not using named arguments in Objective-C makes your code much harder to read.
这篇关于dateFormat 中的错误返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!