2016-04-26 15 views
-5

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 dateFromStringnil 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)

+3

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

+0

Nein, nichts damit zu tun. Dieser wurde von JAL beantwortet! –

+0

Dies ist ein anderes Problem. Aber die DateFormat-Klasse wird überall verwendet, deshalb sehen Sie wieder Code. –

Antwort

2

Lassen Sie mich für Sie Ihren Code erklären:

NSString *isoFormat = ISO8601DateFormatType; 

(ordnet Zeichenfolge ISO8601 zu isoFormat)

NSString *dateFormat = (isoFormat != nil) ? isoFormat : ISO8601DateFormatType; 

(isoFormat ist nie nil so die Bedingung ist immer wahr, wenn sie falsch wäre, würden wir wieder den String ISO8601 zuweisen.

NSDateFormatter *formatter = ... 

(wir einige Formatierer zu bekommen, ist es egal, wie denn wir alle seine wichtigen Eigenschaften ohnehin überschreiben)

formatter.locale = [NSLocale localeWithLocaleIdentifier: (@"en_US_POSIX")]; 

(lassen Sie uns das [NSLocale currentLocale] oben mit POSIX locale überschreiben, das ist in Ordnung)

formatter.timeZone = [NSTimeZone localTimeZone]; 

(wir Zeitzone wieder eingestellt)

formatter.dateFormat = dateFormat; 

(let-Set String ISO8601 das Datumsformat sein, das ist kein gültiges Datumsformat)

NSDate *date = [formatter dateFromString:(string)]; 

(lassen Sie uns versuchen, ein Datumsformat mit einem ungültigen Datum ISO8601 zu analysieren. Offensichtlich sind diese nil zurück)

Kurz gesagt - das Problem ist genau das gleiche wie das Problem in Ihrer vorherigen Frage Error in dateFormat returns nil

+0

@ Coder1000 Weisen Sie Ihrem 'Formatierer' ein korrektes Datumsformat zu. Wenn Sie eine bessere Antwort wünschen, müssen Sie zuerst eine bessere Frage stellen. – Sulthan

+0

Meine Frage läuft im Grunde auf: Wie konvertiert man 'case .ISO8601 (lassen Sie isoFormat):'? – Coder1000

+0

@ Coder1000 Es ist schwer zu wissen, ohne Ihre Implementierung zu wissen, aber vielleicht möchten Sie 'NSString * isoFormat = format.formatDetails;'? – Sulthan

Verwandte Themen