2016-04-02 80 views
0

我有一個問題,這個迅速枚舉轉換成Objective-C的:如何將Swift enum:String轉換爲Objective-C枚舉:NSString?

public enum ISO8601Format: String { 

case Year = "yyyy" // 1997 
case YearMonth = "yyyy-MM" // 1997-07 
case Date = "yyyy-MM-dd" // 1997-07-16 
case DateTime = "yyyy-MM-dd'T'HH:mmZ" // 1997-07-16T19:20+01:00 
case DateTimeSec = "yyyy-MM-dd'T'HH:mm:ssZ" // 1997-07-16T19:20:30+01:00 
case DateTimeMilliSec = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" // 1997-07-16T19:20:30.45+01:00 

    init(dateString:String) { 
     switch dateString.characters.count { 
     case 4: 
      self = ISO8601Format(rawValue: ISO8601Format.Year.rawValue)! 
     case 7: 
      self = ISO8601Format(rawValue: ISO8601Format.YearMonth.rawValue)! 
     case 10: 
      self = ISO8601Format(rawValue: ISO8601Format.Date.rawValue)! 
     case 22: 
      self = ISO8601Format(rawValue: ISO8601Format.DateTime.rawValue)! 
     case 25: 
      self = ISO8601Format(rawValue: ISO8601Format.DateTimeSec.rawValue)! 
     default:// 28: 
      self = ISO8601Format(rawValue: ISO8601Format.DateTimeMilliSec.rawValue)! 
     } 
    } 
} 

public enum DateFormat { 
case ISO8601(ISO8601Format?), DotNet, RSS, AltRSS, Custom(String) 
} 

我已經無處不在研究這一點,並發現這一點,這一點:

Best way to enum NSString

enum Values to NSString (iOS)

沒」我非常理解這些答案。我正在尋找更現代的Objective-C語法的更優雅的解決方案(如果有的話)或更好地解釋這些答案。

謝謝!

+0

我真的不明白枚舉的目的。這是一個非常奇怪的使用枚舉。 – Sulthan

+0

似乎並不奇怪*使用枚舉給我(看蘋果已經顯示[用enum代表條形碼](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/) Swift_Programming_Language/Enumerations.html#// apple_ref/DOC/UID/TP40014097-CH12-ID148)!)。沒有任何「優雅的解決方案」將其轉換爲Objective-C。在C語言中,枚舉只是榮耀的整數 - 不像在Swift中他們更強大。您必須將enum包含在額外的邏輯中才能將枚舉的整數值轉換爲字符串,以作爲鏈接顯示的問題的答案。 – Hamish

+1

雖然我很困惑,爲什麼你要做self = ISO8601Format(rawValue:ISO8601Format.Year.rawValue)!'....爲什麼不只是'self = .Year'? – Hamish

回答

0
NSString * const ISO8601DateFormatType = @"ISO8601"; 
NSString * const DotNetDateFormatType = @"DotNet"; 
NSString * const RSSDateFormatType = @"RSS"; 
NSString * const AltRSSDateFormatType = @"AltRSS"; 
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 

@interface DateFormat : NSObject 

    + (instancetype) ISODateFormat: (NSString *) isoFormat; 
    + (instancetype) DotNetDateFormat; 
    + (instancetype) RSSDateFormat; 
    + (instancetype) AltRSSDateFormat; 
    + (instancetype) CustomDateFormat: (NSString *) formatString; 

    @property (readonly) NSString *dateFormatType; 
    @property (readonly) NSString *formatDetails; 

@end 

@implementation DateFormat 

- (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