如何在Objective-C中使用此枚举?

时间:2016-04-21 13:38:18

标签: objective-c swift enums

1)我从这个转换了这个Swift枚举:

public enum TimeZone {
    case Local, UTC
}

进入:

@interface DateFormat : NSObject

@property enum TimeZone {Local, UTC};


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

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


@end

2)现在我需要转换它:

func toString(format format: DateFormat, timeZone: TimeZone = .Local) -> String
{
    var dateFormat: String
    let zone: NSTimeZone

    switch format {

        case .DotNet:
            let offset = NSTimeZone.defaultTimeZone().secondsFromGMT / 3600
            let nowMillis = 1000 * self.timeIntervalSince1970
            return  "/Date(\(nowMillis)\(offset))/"

        case .ISO8601(let isoFormat):
            dateFormat = (isoFormat != nil) ? isoFormat!.rawValue : ISO8601Format.DateTimeMilliSec.rawValue
            zone = NSTimeZone.localTimeZone()

        case .RSS:
            dateFormat = RSSFormat
            zone = NSTimeZone.localTimeZone()

        case .AltRSS:
            dateFormat = AltRSSFormat
            zone = NSTimeZone.localTimeZone()

        case .Custom(let string):

            switch timeZone {
                case .Local:
                    zone = NSTimeZone.localTimeZone()
                case .UTC:
                    zone = NSTimeZone(forSecondsFromGMT: 0)
            }
            dateFormat = string
    }

    let formatter = NSDate.formatter(format: dateFormat, timeZone: zone)
    return formatter.stringFromDate(self)
}

3)这是我到目前为止转换的内容:

    - (NSString *) toString: (DateFormat *) format : (DateFormat *) timeZone{
        NSString *dateFormat;
        NSTimeZone *zone;
        if([format.dateFormatType compare: DotNetDateFormatType] == NSOrderedSame) {
            NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT] / 3600;
            NSInteger nowMillis = 1000 * self.timeIntervalSince1970;
            return [NSString stringWithFormat:@"/Date(%ld%ld)/", (long)nowMillis, (long)offset];
        }
        else if([format.dateFormatType compare: ISO8601DateFormatType] == NSOrderedSame) {
            NSString *isoFormat = ISO8601DateFormatType;
            dateFormat =  (isoFormat != nil) ? isoFormat : ISOFormatDateTimeMilliSec;
        }
        else if([format.dateFormatType compare: RSSDateFormatType] == NSOrderedSame) {
            dateFormat = RSSDateFormatType;
        }
        else if([format.dateFormatType compare: AltRSSDateFormatType] == NSOrderedSame) {
            dateFormat = AltRSSDateFormatType;
        }
        else if([format.dateFormatType compare: CustomDateFormatType] == NSOrderedSame) {
            NSString *string = CustomDateFormatType;
            dateFormat = string;
        }
        NSDateFormatter *formatter = [NSDate formatter : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];
        return [formatter stringFromDate:(self)];
    }

问题:

我在那里包含TimeZone枚举时遇到了麻烦。我转换的所有行为都很好,我只需要实现枚举。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

而不是内联enum声明:

@property enum TimeZone {Local, UTC};

将其声明为@interface以外的

typedef NS_ENUM(NSInteger, TimeZone) {
   kTimeZoneLocal = 0,
   kTimeZoneUTC
};

(需要名称来防止冲突,第一个值的默认值为零)。

然后属性:

@property (nonatomic) TimeZone timezone;

方法标题应该是这样的:

- (NSString *)toStringWithFormat:(DateFormat *)format andTimeZone:(TimeZone)timeZone {

并且只使用kTimeZoneLocal代替每个.LocalkTimeZoneUTC,而不是每.UTC