Ios 如何在uitableview中获取此类时区列表?

Ios 如何在uitableview中获取此类时区列表?,ios,objective-c,timezone,gmt,Ios,Objective C,Timezone,Gmt,我正在处理一个应用程序,我必须在表视图中显示这种类型的时区列表 我试过这个:- NSMutableArray *timeZoneArray = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]]; NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init]; for (int cou

我正在处理一个应用程序,我必须在
表视图中显示这种类型的
时区
列表

我试过这个:-

 NSMutableArray *timeZoneArray  = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]];
            NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init];

            for (int count = 0; count < [timeZoneArray count] - 1; count = count + 1) {
                NSString *timeZoneStr = [timeZoneArray objectAtIndex:count];
                [abbreviationArray addObject: [timeZoneStr stringByAppendingFormat:@"(%@)",[[NSTimeZone timeZoneWithName: timeZoneStr] abbreviation]]];
            }
            NSLog(@"Abbreviation : %@",abbreviationArray);
NSMutableArray*timeZoneArray=[[NSMutableArray alloc]initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray*abbreviationArray=[[NSMutableArray alloc]init];
对于(整数计数=0;计数<[timeZoneArray计数]-1;计数=计数+1){
NSString*timeZoneStr=[timeZoneArray对象索引:计数];
[abbreviationArray addObject:[TimeZoneStringByAppendingFormat:@“(@)”,[NSTimeZone timeZoneWithName:TimeZoneStringByAppendingFormat]缩写]];
}
NSLog(@“缩写:%@”,缩写数组);
我得到了这种类型的列表

但是我想要这种类型的列表

请帮助我

NSMutableArray*timeZoneArray=[[NSMutableArray alloc]initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray *timeZoneArray  = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init];

for (int count = 0; count < [timeZoneArray count] - 1; count = count + 1) {
NSString *timeZoneStr = [timeZoneArray objectAtIndex:count];
 [abbreviationArray addObject: [timeZoneStr stringByAppendingFormat:@"(%@)",[[NSTimeZone timeZoneWithName: timeZoneStr] abbreviation]]];
}
NSLog(@"Abbreviation : %@",abbreviationArray);
NSMutableArray*abbreviationArray=[[NSMutableArray alloc]init]; 对于(整数计数=0;计数<[timeZoneArray计数]-1;计数=计数+1){ NSString*timeZoneStr=[timeZoneArray对象索引:计数]; [abbreviationArray addObject:[TimeZoneStringByAppendingFormat:@“(@)”,[NSTimeZone timeZoneWithName:TimeZoneStringByAppendingFormat]缩写]]; } NSLog(@“缩写:%@”,缩写数组);
参考链接:


在iOS框架中,没有任何方法可以直接为您提供这种类型的列表

对于这两个选项,可能是: 你必须保持一个稳定的心态。(但随着白天的变化,它将失败)

您的API提供商向您提供此列表

或者直接将其用作API

更多

另一种方式


GoogleAPI这个
NSTimeZone
类将提供很多您需要的东西,这是一个,或者在Xcode中阅读它

class方法
+knownTimeZoneNames
将给出系统的已知时区名称列表-您可以在该列表中看到类似“America/New_York”的名称

使用这些名称,您可以为每个名称创建一个
NSTimeZone
实例

这些实例有一个属性
secondsFromGMT
,它将为您提供格式化GMT偏移量所需的数据

有一种方法
localizedName:locale:
,它将以比“America/New_York”更漂亮的多种样式为您提供时区名称。如果这些样式都不适合你,那么你就必须为这个名字编写自己的漂亮打印机

上面将为您提供一个
NSTimeZone
实例数组,您可以使用
NSComparator
块对其进行排序(请参阅)。您有一个双键排序,主键是GMT偏移量,次键是名称(使用本地化比较)

把以上内容放在一起,你就有了你的区域列表


HTH

试试这个,它返回的结果如下

"(GMT+00:00)Africa/Abidjan"

NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
    [dateFormatter setTimeZone:timeZone];
    NSString *dateString = [dateFormatter stringFromDate: myDate];
    NSMutableString *mu = [NSMutableString stringWithString:dateString];
    [mu insertString:@":" atIndex:3];
    NSString *strResult = [NSString stringWithFormat:@"(GMT%@)%@",mu,obj];
    [arrResult addObject:strResult];
}];
NSLog(@"%@", arrResult);
请尝试以下代码:

NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
    [dateFormatter setTimeZone:timeZone];
    NSString *dateString = [dateFormatter stringFromDate: myDate];
    NSMutableString *mu = [NSMutableString stringWithString:dateString];
    [mu insertString:@":" atIndex:3];
    NSString *strResult = [NSString stringWithFormat:@"(GMT%@)%@",mu,obj];
    [arrResult addObject:strResult];
}];

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self"  ascending: NO];

NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[arrResult sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]];
NSMutableArray *sortArr = [[NSMutableArray alloc] init];

for (int i = 0 ; i < arr.count; i++) {
    NSString *str = [arr objectAtIndex:i];
    NSRange range = [str rangeOfString:@"+"];
    if (range.location == NSNotFound)
    {
        [sortArr addObject:str];
    }
}
for (int i = arr.count -1 ; i >= 0; i--) {
    NSString *str = [arr objectAtIndex:i];
    NSRange range = [str rangeOfString:@"+"];
    if (range.location != NSNotFound)
    {
        [sortArr addObject:str];
    }

}
NSLog(@"%@",sortArr);

首先获取所有可用时区列表,并从GMT获取它们各自的时间间隔。将它们添加到字典中,对字典进行排序并打印它们:

-(void) printTimeZones 
{
    NSLog(@"\n-------Time Zone Test Started-----------");


    NSMutableDictionary *timeZoneValuesDict = [[NSMutableDictionary alloc] init];


    // getting all timezones dict
    NSDictionary *dict = [NSTimeZone abbreviationDictionary];
    for (NSString *key in [dict allKeys])
    {
       // getting (gmt_diff + values) in a dict
       NSTimeZone *timeZK = [NSTimeZone timeZoneWithAbbreviation:key];
       if(timeZK)
    {
        NSInteger secFromGmt = [timeZK secondsFromGMT];
        NSNumber *number = [NSNumber numberWithInteger:secFromGmt];

        // getting gmt time and values in a timeZoneValuesDict
        [timeZoneValuesDict setObject:number forKey:key];
    }
}

NSLog(@"\n--------Available Time Zones----------");

// sorting the timeZoneValuesDict
[timeZoneValuesDict keysSortedByValueUsingComparator:^NSComparisonResult(NSNumber *number1, NSNumber *number2) {
    NSInteger num1 = [number1 integerValue];
    NSInteger num2 = [number2 integerValue];

    if(num1 > num2)
        return NSOrderedAscending;
    else if(num1 < num2)
        return NSOrderedDescending;

    return NSOrderedSame;
}];

// printing the time zones
for (NSString *key in [timeZoneValuesDict allKeys])
{
    NSNumber *number = [timeZoneValuesDict valueForKey:key];
    NSInteger secFromGmt = [number integerValue];
    NSString *value = [dict valueForKey:key];
    NSString *timeZoneStr = [self convertToStringSeconds:secFromGmt andTimeZoneKey:value];
    NSLog(@"%@", timeZoneStr);
}


NSLog(@"\n-------Time Zone Test End-----------");
}

-(NSString *) convertToStringSeconds:(NSInteger)timeInseconds andTimeZoneKey:(NSString *) key {

// converting time
bool isTimeZoneNegative = FALSE;
if(timeInseconds < 0) {
    isTimeZoneNegative = TRUE;
    timeInseconds = -1 * timeInseconds; // converting to positive value
}

NSInteger time_ = timeInseconds;
// convert seconds to hours and minutes
int seconds = time_ % 60;
time_ /= 60;
int minutes = time_ % 60;
time_ /= 60;
int hours = time_ % 24;
time_ /= 24;

NSString *strToRet = @"(GMT ";
if(isTimeZoneNegative)
    strToRet = [strToRet stringByAppendingString:@"-"];
else
    strToRet = [strToRet stringByAppendingString:@" "];

if(hours <= 9)
    strToRet = [strToRet stringByAppendingFormat:@"0%d",hours];
else
    strToRet = [strToRet stringByAppendingFormat:@"%d",hours];

if(minutes <= 9)
    strToRet = [strToRet stringByAppendingFormat:@":0%d",minutes];
else
    strToRet = [strToRet stringByAppendingFormat:@":%d",minutes];

strToRet = [strToRet stringByAppendingString:@")"];


// adding time zone name
NSRange range = [key rangeOfString:@"/"];
if(range.location != NSNotFound)
{
    NSString *tzName = [key substringFromIndex:(range.location + 1)];
    strToRet = [strToRet stringByAppendingFormat:@" %@",tzName];
}

return strToRet;
}
-(无效)打印时区
{
NSLog(@“\n-------时区测试已开始-----------------”;
NSMutableDictionary*timeZoneValuesDict=[[NSMutableDictionary alloc]init];
//获取所有时区的命令
NSDictionary*dict=[NSTimeZone缩写字典];
for(NSString*输入[dict allKeys])
{
//在dict中获取(gmt_diff+值)
NSTimeZone*timeZK=[NSTimeZone TimeZoneWithAbversion:key];
if(timeZK)
{
NSInteger secFromGmt=[timeZK seconds fromgmt];
NSNumber*number=[NSNumber number WITHINTEGER:secFromGmt];
//获取时区值ICT中的gmt时间和值
[timeZoneValuesDict setObject:number-forKey:key];
}
}
NSLog(@“\n-------可用时区------------”;
//对timeZoneValuesDict进行排序
[时区值ICT键使用比较器按值排序:^n比较结果(NSNumber*number1,NSNumber*number2){
NSInteger num1=[number1整数值];
NSInteger num2=[number2 integerValue];
如果(num1>num2)
回报率下降;
否则如果(num1
输出

(GMT) 阿比让
(GMT) 阿克拉
(GMT+3) 亚的斯亚贝巴
(GMT+1) 阿尔及尔
(GMT+3) 阿斯马拉
(GMT) 巴马科
(GMT+1) 班吉

GMT,“GMT+2”,“GMT+1”,“GMT+1”,HADT,AKDT,AST,AST,我从您的代码中获取此信息此方法仅显示可用时区列表。“欧洲/贝尔格莱德(GMT+2)”,“欧洲/柏林(GMT+2)”,“欧洲/布拉迪斯拉发(GMT+2)”,“欧洲/布鲁塞尔(GMT+2)”,“欧洲/布加勒斯特(GMT+3)”,“欧洲/布达佩斯(GMT+2)”,在你的列表中看到这个,但在上图中看到,@PayalManiyar检查此链接可能会对你有所帮助。很好,我想你明白我说的是哪种类型的列表,我的意思是,如果我忽略GMT,此列表是州、国家和岛屿的混合列表?是的,它是混合列表这是一个有趣的问题
TimeZone.knownTimeZoneIdentifiers
.map { TimeZone(identifier: $0) }
.compactMap{ $0 }.map { timeZone -> String in
    let formatter = DateFormatter()
    formatter.dateStyle = .long
    formatter.dateFormat = "VVV"
    formatter.timeZone = timeZone
    formatter.locale = Locale(identifier: "zh")
    return "(\(timeZone.abbreviation() ?? "")) \(formatter.string(from: Date()))"
}.forEach {
    print($0)
}
(GMT) 阿比让
(GMT) 阿克拉
(GMT+3) 亚的斯亚贝巴
(GMT+1) 阿尔及尔
(GMT+3) 阿斯马拉
(GMT) 巴马科
(GMT+1) 班吉