如何将CLLCoordinate2D lat / long转换为NSNumber

时间:2015-07-24 17:03:26

标签: ios objective-c generics nsnumber

我正在尝试使用Yelp API,并且我试图将lat / long作为API搜索的参数。但是,它不采用类型double,它只接受Objective-C对象。不了解Objective-C,你对lat和long参数的类型有什么建议?我尝试过NSNumber,但当我尝试将CLLocationCoordinate2D类型的纬度/经度坐标转换为接受双精度的NSNumber时,其值为nil

这是我正在使用的Yelp API:

- (void)queryTopBusinessInfoForTerm:(NSString *)term location:(NSString *)location latitude:(NSNumber *)latitude longitude:(NSNumber *)longitude completionHandler:(void (^)(NSDictionary *topBusinessJSON, NSError *error))completionHandler {

  NSLog(@"Querying the Search API with term \'%@\' and location \'%@'", term, location);

  //Make a first request to get the search results with the passed term and location
  NSURLRequest *searchRequest = [self _searchRequestWithTerm:term location:location latitude:latitude longitude:longitude];
  NSURLSession *session = [NSURLSession sharedSession];
  [[session dataTaskWithRequest:searchRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

if (!error && httpResponse.statusCode == 200) {

  NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  NSArray *businessArray = searchResponseJSON[@"businesses"];

  if ([businessArray count] > 0) {

    NSDictionary *firstBusiness = [businessArray firstObject];
    NSString *firstBusinessID = firstBusiness[@"id"];
    NSLog(@"%lu businesses found, querying business info for the top result: %@", (unsigned long)[businessArray count], firstBusinessID);

    [self queryBusinessInfoForBusinessId:firstBusinessID completionHandler:completionHandler];
  } else {
    completionHandler(nil, error); // No business was found
  }
} else {
  completionHandler(nil, error); // An error happened or the HTTP response is not a 200 OK
}
  }] resume];
}

这是params

- (NSURLRequest *)_searchRequestWithTerm:(NSString *)term location:(NSString *)location latitude:(NSNumber *) latitude longitude:(NSNumber *)longitude {
  NSDictionary *params = @{
                       @"term": term,
                       @"location": location,
                       @"cll": latitude,
                       @"cll": longitude,
                       @"limit": kSearchLimit
                       };

  return [NSURLRequest requestWithHost:kAPIHost path:kSearchPath params:params];
}

这是我当前的Swift方法从YelpAPi调用查询:

func yelpApi() {
    var latitude = NSNumber(double: businessStreetAddress.latitude)
    var longitude = NSNumber(double: businessStreetAddress.longitude)
    var searchTerm: NSString = "Asian Food";
    var defaultLocation: NSString = "New York"
    var APISample:YPAPISample = YPAPISample();

    var requestGroup:dispatch_group_t = dispatch_group_create();


    APISample.queryTopBusinessInfoForTerm(searchTerm as String, location: defaultLocation as String, latitude: latitude, longitude: longitude) { (topBusinessJSON: [NSObject: AnyObject]!, error: NSError!) -> Void in
        if((error) != nil) {
            println("Error happened during the request" + error.localizedDescription);
        } else if((topBusinessJSON) != nil) {
            println("Top business info",topBusinessJSON);
        } else {
            println("No business was found");
        }

        dispatch_group_leave(requestGroup);
    }

    dispatch_group_wait(requestGroup, DISPATCH_TIME_FOREVER);


}

1 个答案:

答案 0 :(得分:2)

要将CLLocationCoordinate2D转换为NSNumber,您无法将其NSNumber转换为NSNumber。您可以转换为两个 CLLocationCoordinate2D location; // This is the location you have. NSNumber *latitude = [NSNumber numberWithDouble:location.latitude]; NSNumber *longitude = [NSNumber numberWithDouble:location.longitude]; 对象,如下所示:

 CLLocationCoordinate2D location; // This is the location you have.
 NSNumber *latitude = @(location.latitude);
 NSNumber *longitude = @(location.longitude);

使用Modern ObjC,您可以隐藏为:

NSURLRequest *searchRequest = [self _searchRequestWithTerm:term location:location latitude:latitude longitude:longitude];

并致电您的

location

不要与名为searchRequestWithTerm:location:latitude:longitude:的变量混淆,我只是命名它。因为您的函数location有一个名为NSString的参数,它接受[Flags] enum People { Adam = 0x1, Barry = 0x2, Chris = 0x4, David = 0x8, Eric = 0x10, Frank = 0x20, George = 0x40, Harold = 0x80 }; Dictionary<string, People> EUInterest = new Dictionary<string, People>() { { "athletes", People.Adam | People.Barry }, { "artists", People.Frank | People.Harold } }; Dictionary<string, People> USInterest = new Dictionary<string, People>() { { "athletes", People.Chris | People.Harold }, { "artists", People.Eric } }; var result = from interest in EUInterest.Keys where USInterest.ContainsKey(interest) let v1 = EUInterest[interest] let v2 = USInterest[interest] select new Dictionary<string, People>() { {interest, v1 | v2 } };

这可能会对你有帮助。