0

I am having an issue converting this swift enum to 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)
}

I already researched this everywhere and found this and this:

Best way to enum NSString

enum Values to NSString (iOS)

Didn't quite understand those answers.I am looking for more elegant solutions (if there are any) or better explanations of those answers with modern objective-c syntax.

Thank you !

Community
  • 1
  • 1
Question
  • 3
  • 3
  • I don't really understand the purpose of the enum. It's a very strange usage of an enum. – Sulthan Apr 02 '16 at 11:55
  • Doesn't seem *that* strange a use of enums to me (seeing as Apple has shown [representing a barcode with an enum](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-ID148)!). There isn't going to be any "elegant solution" for converting it to Objective-C. In C, enums are just glorified integers - unlike in Swift where they're *way* more powerful. You'll have to wrap the enum in extra logic to convert an integer value of your enum to a string, as the answers to the questions you linked to show. – Hamish Apr 02 '16 at 12:58
  • 1
    Although I'm very confused why you're doing `self = ISO8601Format(rawValue: ISO8601Format.Year.rawValue)!`.... why not just `self = .Year`? – Hamish Apr 02 '16 at 13:08

1 Answers1

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
Scott Thompson
  • 22,629
  • 4
  • 32
  • 34