我遇到了将此快速枚举转换为Objective-C的问题:
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)
}
我已经在各地研究了这个,发现了这个和这个:
我不太明白这些答案。我正在寻找更优雅的解决方案(如果有的话)或用现代的objective-c语法更好地解释这些答案。
谢谢!
答案 0 :(得分: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