注意: Swift代码不是我的。我只是将它转换为Objective-C作为挑战:D
问题:您如何将以下这些枚举转换为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)
}
我做了什么:
#import <Foundation/Foundation.h>
extern NSString * const ISO8601DateFormatType;
extern NSString * const DotNetDateFormatType;
extern NSString * const RSSDateFormatType;
extern NSString * const AltRSSDateFormatType;
extern NSString * const CustomDateFormatType;
extern NSString * const ISOFormatYear;
extern NSString * const ISOFormatYearMonth; // 1997-07
extern NSString * const ISOFormatDate; // 1997-07-16
extern NSString * const ISOFormatDateTime; // 1997-07-16T19:20+01:00
extern NSString * const ISOFormatDateTimeSec; // 1997-07-16T19:20:30+01:00
extern NSString * const ISOFormatDateTimeMilliSec; // 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
#import "DateFormat.h"
@implementation DateFormat
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
- (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
答案 0 :(得分:3)
我在Objective-C方面有点生疏,所以可能有更简单的方法来编写代码。这一切都是在Swift代码中完成的,它被桥接到Objective-C:
import Foundation
/// enum ISO8601Format String for Swift
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
}
/// Bridges ISO8601Format to class constants for Objective-C
@objc(ISO8601Format)
class ISO8601FormatBridge: NSObject {
static let Year = ISO8601Format.Year.rawValue
static let YearMonth = ISO8601Format.YearMonth.rawValue
static let Date = ISO8601Format.Date.rawValue
static let DateTime = ISO8601Format.DateTime.rawValue
static let DateTimeSec = ISO8601Format.DateTimeSec.rawValue
static let DateTimeMilliSec = ISO8601Format.DateTimeMilliSec.rawValue
}
/// enum DateFormat for Swift
public enum DateFormat {
case ISO8601(ISO8601Format?)
case DotNet
case RSS
case AltRSS
case Custom(String)
/// Strings for each case
var name: String {
switch self {
case ISO8601 : return "ISO8601"
case DotNet : return "DotNet"
case RSS : return "RSS"
case AltRSS : return "AltRSS"
case Custom : return "Custom"
}
}
}
/// Bridges DateFormat to class factory methods for Objective-C
@objc(DateFormat)
class DateFormatBridge: NSObject {
class func ISO8601(details:String?) -> DateFormatBridge {
return DateFormatBridge(DateFormat.ISO8601(nil).name, details: details)
}
class func DotNet() -> DateFormatBridge {
return DateFormatBridge(DateFormat.DotNet.name, details: nil)
}
class func RSS() -> DateFormatBridge {
return DateFormatBridge(DateFormat.RSS.name, details: nil)
}
class func AltRSS() -> DateFormatBridge {
return DateFormatBridge(DateFormat.AltRSS.name, details: nil)
}
class func Custom(details: String) -> DateFormatBridge {
return DateFormatBridge(DateFormat.Custom("").name, details: details)
}
/// Type of Swift enum DateFormat
let type: String
/// Associated value of the type
let details: String?
/// Create instances with associated values
private init(_ format: String, details: String?) {
self.type = format
self.details = details
}
}
可以像这样使用:
NSLog(@"%@", [ISO8601Format Year]); // => "yyyy"
NSLog(@"%@", [ISO8601Format YearMonth]); // => "yyyy-MM"
DateFormat * date = [DateFormat ISO8601:[ISO8601Format Year]];
NSLog(@"%@, %@", [date type], [date details]); // => "ISO8601, yyyy"