Coder1000 Coder1000 - 7 months ago 56
Swift Question

How do I use this enum in Objective-C?

I converted this Swift enum from this:

public enum TimeZone {
case Local, UTC
}


into this:

@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


Now I need to convert this:

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)
}


Here's what I converted so far:

- (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)];
}


I have trouble including the TimeZone enum in there. All behaviour that I converted works fine, I just need to implement the enum. Any ideas?

Answer

Instead of inline enum declaration:

@property enum TimeZone {Local, UTC};

Declare it outside the @interface as

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

(names are needed to prevent conflict, default value of zero for the first value is common).

Then the property:

@property (nonatomic) TimeZone timezone;

The method header should be something like this:

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

and just use kTimeZoneLocal instead of every .Local and kTimeZoneUTC instead of every .UTC.