Coder1000 Coder1000 - 1 year ago 95
Swift Question

Error in dateFormat returns nil

EDIT: I heavily modified my question after the downvote it got. Please consider removing the downvote.

As an exercise to practice Objective-C, I am converting Swift code to Objective-C.
After 1.5k lines converted, I find myself blocked since days on one last issue.
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.

After much frustration, searches, and hours of debugging trial and error, here is my question:

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];


This is the cell data that doesn't load correctly (empty strings are passed to

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 somewhere:

Original Swift syntax:

toString() function

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

What I tried:


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

Answer Source

The problem is in this line:

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

dateFormat is @"Custom", as it was set here:

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

This is passed into your NSDateFormatter constructor here:

formatter.dateFormat = format;

@"Custom" is not a valid date format string. Instead, pass in format.formatDetails:

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

I should note that not using named arguments in Objective-C makes your code much harder to read.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download