gpt4 book ai didi

ios - Objective-C 变量被设置为 null

转载 作者:行者123 更新时间:2023-11-29 10:31:29 24 4
gpt4 key购买 nike

所以我遇到了一个问题,我的代码中的一个变量在设置为非 null 值后变为 null。变量设置在这里:

MKLocalSearchCompletionHandler completionHandler = ^(MKLocalSearchResponse *response, NSError *error) {
self.places = [response mapItems];
};

当我在此代码段之外调用 self.places 时,self.places 为空。有什么建议吗?

这是类(为了简洁我去掉了一些方法):

#import "AddFenceController.h"
#import "PlaceAnnotation.h"

@interface AddFenceController () <UISearchBarDelegate, UISearchControllerDelegate, CLLocationManagerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSMutableArray *locations;

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) MKLocalSearchRequest *localSearchRequest;
@property (nonatomic, strong) MKLocalSearch *localSearch;
@property CLLocationCoordinate2D coords;
@property (nonatomic, strong) PlaceAnnotation *annotation;

@property (nonatomic, strong) NSArray *places;

@end

@implementation AddFenceController {
}

@synthesize latitude;
@synthesize longitude;
@synthesize region;
@synthesize radius;
@synthesize geofence;
@synthesize places;

- (void)viewDidLoad {
[self.ibSearchBar setDelegate:self];

self.latitude = [self.geofence latitude];
self.longitude = [self.geofence longitude];
self.radius = [self.geofence radius];
}

- (void)startSearch:(NSString *)searchString
{
if (self.localSearch.searching)
{
[self.localSearch cancel];
}

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];

request.naturalLanguageQuery = searchString;

MKLocalSearchCompletionHandler completionHandler = ^(MKLocalSearchResponse *response, NSError *error)
{
if (error != nil)
{
NSString *errorStr = [[error userInfo] valueForKey:NSLocalizedDescriptionKey];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Could not find places"
message:errorStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
self.places = [response mapItems];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
};

if (self.localSearch != nil)
{
self.localSearch = nil;
}
self.localSearch = [[MKLocalSearch alloc] initWithRequest:request];

[self.localSearch startWithCompletionHandler:completionHandler];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.region = [self setUpGeofence:self.geofence.latitude.doubleValue:self.geofence.longitude.doubleValue];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[self startSearch:self.ibSearchBar.text];

MKMapItem *mapItem = [self.places objectAtIndex:0];
self.latitude = [NSNumber numberWithDouble:mapItem.placemark.coordinate.latitude];
self.longitude = [NSNumber numberWithDouble:mapItem.placemark.coordinate.longitude];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeNone];


....

}

....

@end

最佳答案

你的程序流程有问题:当你调用

[self startSearch:self.ibSearchBar.text];

您开始搜索,然后立即返回。在下一个语句中访问 self.places 还为时过早,即在这里

MKMapItem *mapItem = [self.places objectAtIndex:0];

因为搜索没有机会返回!

处理此问题的正确方法是向您的类添加另一个处理搜索完成的方法,并在设置 self.places 后调用它:

// Make this change in your asynchronous handler:
if (error != nil) {
....
[alert show];
} else {
self.places = [response mapItems];
// Add this line
[self searchBarSearchCompleted];
}
....
// Add this method to your class
- (void)searchBarSearchCompleted {
MKMapItem *mapItem = [self.places objectAtIndex:0];
self.latitude = [NSNumber numberWithDouble:mapItem.placemark.coordinate.latitude];
self.longitude = [NSNumber numberWithDouble:mapItem.placemark.coordinate.longitude];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeNone];
....
}

这有效地将您的 searchBarSearchButtonClicked: 方法拆分为“之前”和“之后”部分。 “之前”部分保持不变 - 它告诉 startSearch: 开始搜索。

“之后”部分被移动到一个单独的方法中。它在搜索完成后接管。如果您不需要 searchBarSearchCompleted 之外的 places,您可以完全删除变量,并将数组作为参数传递。

关于ios - Objective-C 变量被设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29371997/

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