HINWEIS: Als Übung konvertiere ich Swift-Code zu Objective-C.Wie konvertiere ich eine parametrisierte enum/enum mit zugehörigen Werten von Swift zu Objective-C?
Code: Original-Swift-Code, aus dem ich umgewandelt:
case .ISO8601(let isoFormat):
let dateFormat = (isoFormat != nil) ? isoFormat! : ISO8601Format(dateString: string as String)
let formatter = NSDate.formatter(format: dateFormat.rawValue)
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone.localTimeZone()
formatter.dateFormat = dateFormat.rawValue
if let date = formatter.dateFromString(string as String) {
self.init(timeInterval:0, sinceDate:date)
} else {
self.init()
}
, was ich versucht: Die Objective-C Umwandlung Ich habe versucht:
else if([format.dateFormatType compare: ISO8601DateFormatType] == NSOrderedSame) {
NSString *isoFormat = // WHAT DO TO HERE ?;
NSString *dateFormat = (isoFormat != nil) ? isoFormat : ISO8601DateFormatType;
NSDateFormatter *formatter = [NSDate formatterWithFormat: dateFormat andTimeZone: [NSTimeZone localTimeZone ] andLocale: [NSLocale currentLocale]];
formatter.locale = [NSLocale localeWithLocaleIdentifier: (@"en_US_POSIX")];
NSLog([NSString stringWithFormat:@"%@", dateFormat]);
// dateFormat = ISO8601
NSDate *date = [formatter dateFromString:(string)];
//
// ISSUE IS HERRRREEEE^
//
NSLog([NSString stringWithFormat:@"%@", date]);
// date = nil
if (date != nil){
return [self initWithTimeInterval: 0 sinceDate: date];
}
else {
return [self init];
}
}
Der Ansatz, den ich nahm, war eine separate cl zu verwenden ass das Verhalten eines schnellen Enum mit dem dazugehörigen Werten (dies war eine ursprünglich eine große rasche Enum mit zugehörigen Werten) zu reproduzieren:
DateFormat.m
#import "DateFormat.h"
@implementation DateFormat
NSString * const ISO8601DateFormatType = @"ISO8601";
NSString * const DotNetDateFormatType = @"DotNet";
NSString * const RSSDateFormatType = @"EEE, d MMM yyyy HH:mm:ss ZZZ";
NSString * const AltRSSDateFormatType = @"d MMM yyyy HH:mm:ss ZZZ";
NSString * const CustomDateFormatType = @"Custom";
NSString * const ISOFormatYear = @"yyyy";
NSString * const ISOFormatYearMonth = @"yyyy-MM"; // 1997-07
NSString * const ISOFormatDate = @"yyyy-MM-dd"; // 1997-07-16
NSString * const ISOFormatDateTime = @"yyyy-MM-dd'T'HH:mmZ"; // 1997-07-16T19:20+01:00
NSString * const ISOFormatDateTimeSec = @"yyyy-MM-dd'T'HH:mm:ssZ"; // 1997-07-16T19:20:30+01:00
NSString * const ISOFormatDateTimeMilliSec = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ"; // 1997-07-16T19:20:30.45+01:00
- (instancetype) initWithType: (NSString *) formatType details: (NSString *) details {
if(self = [super init]) {
_dateFormatType = formatType;
_formatDetails = details;
}
return self;
}
+ (instancetype) ISODateFormat: (NSString *) isoFormat
{
return [[DateFormat alloc] initWithType: ISO8601DateFormatType details: isoFormat];
}
+ (instancetype) DotNetDateFormat
{
return [[DateFormat alloc] initWithType: DotNetDateFormatType details: nil];
}
+ (instancetype) RSSDateFormat
{
return [[DateFormat alloc] initWithType: RSSDateFormatType details: nil];
}
+ (instancetype) AltRSSDateFormat
{
return [[DateFormat alloc] initWithType: AltRSSDateFormatType details: nil];
}
+ (instancetype) CustomDateFormat: (NSString *) formatString
{
return [[DateFormat alloc] initWithType: CustomDateFormatType details: formatString];
}
@end
MEIN PROBLEM:
In meinem Code gibt dateFromString
nil
zurück, weil das als Parameter übergebene Format falsch ist. Der Grund dafür ist die Art und Weise, wie ich die Enumeration mit der zugehörigen Wertzeile konvertiert habe: case .ISO8601(let isoFormat):
Ich weiß nicht, wie man case .ISO8601(let isoFormat):
in Objective-C umwandeln kann.
Das Problem besteht darin, dass Enums mit zugeordneten Werten nur in Swift, nicht in Objective-C existieren.
Also, wie kann ich diese enum mit zugehörigen Wert Zeile Fall .ISO8601(let isoFormat):
in Objective-C konvertieren?
LÖSUNG:
EDIT: Die Antwort war einfach, ich musste einfach schreiben: NSString *isoFormat = format.formatDetails;
in Objective-C Umwandlung ich zu Beginn der Frage zeigte. (Check out my Klasse Dateformat Implementierungsdatei zu verstehen)
Ist das nicht eine Kopie von http://stackoverflow.com/questions/36753954/ description-label-doesnt-in-my-tableviewcontroller-Zellen? Wir werden Ihnen nicht helfen können, bis Sie [Minimal, Complete und Verifable example] schreiben (http://stackoverflow.com/help/mcve) – Sulthan
Nein, nichts damit zu tun. Dieser wurde von JAL beantwortet! –
Dies ist ein anderes Problem. Aber die DateFormat-Klasse wird überall verwendet, deshalb sehen Sie wieder Code. –