【问题标题】:iOS - [CLPlacemark timeZone]: unrecognized selector iOS 8iOS - [CLPlacemark timeZone]:无法识别的选择器 iOS 8
【发布时间】:2017-07-13 16:51:04
【问题描述】:

我正在使用这种方法从lat/long 获取timeZone

-(void)getTimeZoneFromLatLong
{
    CLLocation *location = [[CLLocation alloc] initWithLatitude:self.parentVC.currentCity.latitude.doubleValue longitude:self.parentVC.currentCity.longitude.doubleValue];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation: location completionHandler:^(NSArray *placemarks, NSError *error)
    {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        _placemark = placemark;
    }];
}

然后我可以使用timeZone 来调用:

EDSunriseSet *eds = [EDSunriseSet sunrisesetWithTimezone:_placemark.timeZone
           latitude:latitude.doubleValue longitude:longitude.doubleValue];

EDSunriseSet 是一个库,用于从 lat/longtimeZone 获取 Sunrise/Sunset 值。

它运行良好,但是Crashlytics 提醒我[CLPlacemark timeZone]iOS8 及更低版本不兼容。

如何调整我的代码以适应 iOS8

编辑: [_placemark timezone]_placemark.timezone 都被接受?

【问题讨论】:

  • 这正是我正在做的事情
  • 不,您没有正确阅读。这个问题是iOS9之前的问题。他们正在使用 CLPlaceMarktimeZone 属性从其 loc/lat 中检索 NSTimeZone(因为它当时不存在)。
  • 我无法结束这个问题。我需要答案。我使用了您链接中的 chris 解决方案。

标签: ios objective-c swift timezone selector


【解决方案1】:

它在 iOS 9 中可用。

/*
     *  timeZone
     *
     *  Discussion:
     *      Returns the time zone associated with the placemark.
     */
    @available(iOS 9.0, *)
    open var timeZone: TimeZone? { get }

对于 iOS 8,试试这个

CLLocation *currentLocaiton = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[geoCoder reverseGeocodeLocation:currentLocaiton completionHandler:^(NSArray *placemarks, NSError *error) { 

if (error == nil && [placemarks count] > 0) {placeMark = [placemarks lastObject]; 
    NSRegularExpression regex = [NSRegularExpression regularExpressionWithPattern:@"identifier = \"[a-z]*\\/[a-z]_*[a-z]*\"" options:NSRegularExpressionCaseInsensitive error:NULL]; 
    NSTextCheckingResult *newSearchString = [regex firstMatchInString:[placeMark description] options:0 range:NSMakeRange(0, [placeMark.description length])]; 
    NSString *substr = [placeMark.description substringWithRange:newSearchString.range]; NSLog(@"timezone %@",substr); 
}

【讨论】:

  • 是的,我的问题是除了 iOS 8 之外,我如何才能实现我正在做的事情。
  • 您也想为 IOS 8 设置时区吗?
  • 我想从 lat/long 获取 iOS 8 的 timeZone 是的。我的代码适用于 iOS 9 及更高版本。
  • 在 iOS 8 中有两种方法可以做到这一点。
    第一种是
    [在此处查找库]github.com/Alterplay/APTimeZones 和 CLLocation *location = [[CLLocation alloc] initWithLatitude:100.12345 longitude :80.12345]; NSTimeZone *timeZone = [[APTimeZones sharedInstance] timeZoneWithLocation:location]; NSLog(@"%@", timeZone);
  • 第二个是 CLLocation *currentLocaiton = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [geoCoder reverseGeocodeLocation:currentLocaiton completionHandler:^(NSArray *placemarks, NSError *error) {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多