gpt4 book ai didi

ios - 为什么我的第一个回复是空的?

转载 作者:行者123 更新时间:2023-11-29 03:16:01 25 4
gpt4 key购买 nike

我有一个 Location.m 模型,我用它来获取纬度和经度。当我拥有这些时,我会调用在我的 Foursquare.m 模型中实现的委托(delegate)方法。

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

位置.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 模型调用委托(delegate)方法时,我将 NSLog 的响应输出到一个数组中(称为 self.dataResponse)。它总是首先输出 (null),然后是所有的 JSON。为什么会这样?如果我直接 NSLog out dataDictionary(见下面的代码),JSON 输出显示错误代码:400 并且不打印任何内容。打印完所有内容后几毫秒,似乎工作正常...

这是一张图片:当我记录 dataDictionary enter image description here

这是在 Foursquare.m 中实现的委托(delegate)方法:

- (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];

最佳答案

据我所知,您问题中的最后一行代码可能是问题所在:

[_foursquare location:_location didFinishFindingLocation:_location.location];

您不应该显式调用您的委托(delegate)方法(在设置位置之前)。相反,您应该等待 CLLocation 回调将其启动。

其他想法:

  • 如果 Location 类有一个 .location 属性,我认为这是令人困惑的。

  • Location.m 不是模型,它是包含类实现的文件。我会说 Location 类是一个模型。

  • 您可以将委托(delegate)方法简化为 didFindLocation:(Location *)location,因为您可以在方法中将 CLLocation 作为 location.location .

  • 房地产最重要的三个因素是什么? location.location.location !

关于ios - 为什么我的第一个回复是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21701175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com