gpt4 book ai didi

objective-c - 如何在 Objective-C 中使用这个枚举?

转载 作者:行者123 更新时间:2023-11-30 10:04:38 26 4
gpt4 key购买 nike

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 枚举时遇到问题。我转换的所有行为都工作正常,我只需要实现枚举。有什么想法吗?

最佳答案

而不是内联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

关于objective-c - 如何在 Objective-C 中使用这个枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36771367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com