0

我在将CCLocationManager方法转换为块时遇到问题。 我CLLocationManager方法是这样的:如何将CLLocationManager方法转换为iOS中的块目标c

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    } 
    else{NSLog(@"current location is nil");}; 
} 

我想这种方法转换成块,但在试图这样做有困难。一种新的客观的C和IOS开发。

我在做什么是这样的:

double (^getSpeed)(*CLLocation, *CLLocation) = ^(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
     { 
      NSLog(@"didUpdateToLocation: %@", newLocation); 
      CLLocation *currentLocation = newLocation; 
      mSpeed = currentLocation.speed; 
      } 
      else{NSLog(@"current location is nil");}; 
      return mSpeed; 
     }; 

,但它给了我一个错误说,它预计在块的第一行的表达式。这是什么意思? 任何帮助,将不胜感激。

回答

0

你可以试试这个方法,在委托方法

// 1.声明的方法和块.h文件中这样

typedef void (^currentLocation)(CLLocation *currentLocation); 
    // Create property using typedef 
    @property (strong) currentLocation objBlock; 
    - (void)updateCurrentLocation:(currentLocation)completionBlock; 

// 2。是这样的

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray<CLLocation *> *)locations { 
    CLLocation *location = [locations lastObject]; 
    _objBlock(location); 
    [locManager stopUpdatingLocation]; 
    locManager = nil; 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error { 
    NSLog(@"%@",[error localizedDescription]); 
} 

- (void)updateCurrentLocation:(currentLocation)completionBlock { 
    _objBlock = completionBlock; 
} 

// 3.调用将是这样的,当任何位置被调用块会得到触发器。

[self updateCurrentLocation:^(CLLocation *currentLocation) { 
     NSLog(@"%@", currentLocation); 
    }]; 
+0

我同意但它不能返回任何东西。基本上我试图在另一个函数中写一个块,它会返回给我一个速度 – Nik391

+0

如果“didUpdateLocations”委托方法被调用“updateCurrentLocation”,则必须返回该位置。检查代表是否正在呼叫。在块中,它可以计算出你想要的速度。 – Vishnuvardhan

+0

感谢您的澄清 – Nik391

相关问题