我有Location.m
模型,用于获取纬度和经度。当我有这些时,我会调用我的Foursquare.m
模型中实现的委托方法。
Location.h看起来像这样:
@class Location;
@protocol LocationDelegate <NSObject>
- (void)location:(Location *)loc didFinishFindingLocation:(CLLocation *)location;
@end
@interface Location : NSObject <CLLocationManagerDelegate>
@property (nonatomic, weak) id <LocationDelegate> delegate;
- (void)startLocationManager;
@end
Location.m:
@implementation Location
{
CLLocationManager *_locationManager;
BOOL _locationIsUpdated;
}
- (void)startLocationManager
{
NSLog(@"In startLocationManager");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
_locationIsUpdated = NO;
[_locationManager startUpdatingLocation];
}
#pragma mark - LocationManager Delegates
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if (locations && _locationIsUpdated == NO) {
self.location = [locations lastObject];
NSLog(@"%@", _location);
_locationIsUpdated = YES;
[self.delegate location:self didFinishFindingLocation:self.location];
}
}
@end
当从Foursquare模型调用委托方法时,我在一个数组中调用响应(称为self.dataResponse)。它总是首先输出(null)
,然后输出所有JSON。为什么会这样?如果我直接登出dataDictionary
(请参阅下面的代码),则JSON输出显示错误代码:400并且不打印任何内容。打印完所有内容后的几毫秒,似乎工作正常......
这是一张图片:
当我记录dataDictionary
时
这是在Foursquare.m中实现的委托方法:
- (void)location:(Location *)loc didFinishFindingLocation:(CLLocation *)location
{
loc.delegate = self;
self.resultFromResponse = [[NSMutableArray alloc] init];
NSString *search = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%f,%f&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=%@&client_secret=%@&v=%@", location.coordinate.latitude, location.coordinate.longitude, kFourdquareAPIKey, fFoursquareSecret, [Foursquare getCurrentDate]];
NSURL *url = [NSURL URLWithString:search];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dataDictionary = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!error) {
self.dataResponse = dataDictionary[@"response"][@"venues"];
NSLog(@"%@", self.dataResponse);
for (NSDictionary *venue in self.dataResponse) {
FSDataFromJSON *data = [[FSDataFromJSON alloc] init];
data.name = venue[@"name"];
data.phone = venue[@"phone"];
data.location = venue[@"location"];
[self.resultFromResponse addObject:data];
}
//[[NSNotificationCenter defaultCenter] postNotificationName:@"JSONRequestFinishedLoading" object:nil];
} else {
NSLog(@"%@", error.description);
}
}];
[task resume];
}
最后,这些模型在我的MainViewController中启动:
_location = [[Location alloc] init];
[_location startLocationManager];
_foursquare = [[Foursquare alloc] init];
[_foursquare location:_location didFinishFindingLocation:_location.location];
答案 0 :(得分:1)
据我所知,问题中的最后一行代码可能是问题所在:
[_foursquare location:_location didFinishFindingLocation:_location.location];
您不应该显式调用您的委托方法(在设置位置之前)。相反,您应该等待CLLocation
回调开始。
其他想法:
如果Location
类具有.location
属性,我认为这令人困惑。
Location.m
不是模型,它是包含类实现的文件。我会说Location
类是模型。
您可以将代理方法简化为didFindLocation:(Location *)location
,因为您可以将方法中的CLLocation
设为location.location
。
房地产中最重要的三个因素是什么? location.location.location
<!/ p>