【问题标题】:How to get TimeZone abbreviation如何获取时区缩写
【发布时间】:2014-07-07 23:30:48
【问题描述】:

我想创建一个时区列表。 但是我在 timeZone.abbreviation 得到了以下错误。

-[__NSCFString abbreviation]:无法识别的选择器发送到实例 0x19cb80b0

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    NSTimeZone *timeZone = [[NSTimeZone knownTimeZoneNames] objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZone.abbreviation;  // <- Error Here
    cell.detailTextLabel.text = timeZone.description;
    cell.accessoryType = (timeZone == self.timeZone)? UITableViewCellAccessoryCheckmark :UITableViewCellAccessoryNone;

    return cell;
}

我尝试在互联网上搜索,但目前找不到解决方案。 请给我一些建议。 提前致谢。

【问题讨论】:

    标签: ios uitableview nstimezone


    【解决方案1】:

    试试这样的:

    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:[[NSTimeZone knownTimeZoneNames] objectAtIndex:indexPath.row]];
    cell.textLabel.text = timeZone.abbreviation;  // <- Error Here
    cell.detailTextLabel.text = timeZone.description;
    

    您当前的代码得到一个 NSStrings 数组,而不是 NSTimeZones。这应该会给你一个 NSTimeZone,然后你就可以得到abbreviation 属性。

    【讨论】:

      【解决方案2】:

      这里提到:https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimeZone_Class/Reference/Reference.html#//apple_ref/occ/clm/NSTimeZone/knownTimeZoneNames

      [NSTimeZone knownTimeZoneNames] 
      

      返回时区的字符串 id 列表 - 因此您的时区对象实际上包含 NSString 而不是 NSTimeZone 对象。所以你必须这样做:

      NSString *timeZoneID = [[NSTimeZone knownTimeZoneNames] objectAtIndex:indexPath.row];
      NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneID];
      

      获取真正的时区对象以调用缩写

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 2020-07-20
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 2022-01-08
        • 1970-01-01
        相关资源
        最近更新 更多