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

相關問題