Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C变量设置为空_Ios_Objective C - Fatal编程技术网

Ios 目标C变量设置为空

Ios 目标C变量设置为空,ios,objective-c,Ios,Objective C,因此,我的代码中的一个变量在设置为非null值后变为null,这是一个问题。变量设置如下: MKLocalSearchCompletionHandler completionHandler = ^(MKLocalSearchResponse *response, NSError *error) { self.places = [response mapItems]; }; 在代码段之外调用self.places时,self.places为null。有什么建议吗 下面是这个类(为了简洁

因此,我的代码中的一个变量在设置为非null值后变为null,这是一个问题。变量设置如下:

MKLocalSearchCompletionHandler completionHandler = ^(MKLocalSearchResponse *response, NSError *error) {
     self.places = [response mapItems];
};
在代码段之外调用self.places时,self.places为null。有什么建议吗

下面是这个类(为了简洁起见,我拿出了一些方法):

#导入“AddFenceController.h”
#导入“PlaceAnnotation.h”
@接口控制器()
@属性(非原子,强)CLLocationManager*locationManager;
@属性(非原子,强)NSMutableArray*位置;
@属性(非原子,强)UISearchController*searchController;
@属性(非原子,强)MKLocalSearchRequest*localSearchRequest;
@属性(非原子,强)MKLocalSearch*localSearch;
@财产定位坐标2 D坐标;
@属性(非原子,强)PlaceAnnotation*annotation;
@属性(非原子、强)NSArray*位置;
@结束
@安全控制器的实现{
}
@综合纬度;
@综合经度;
@合成区域;
@综合半径;
@综合土工格栅;
@综合场所;
-(无效)viewDidLoad{
[self.ibSearchBar setDelegate:self];
self.latitude=[self.geofines latitude];
self.longitude=[self.geofrance-longitude];
self.radius=[self.geofrance radius];
}
-(void)开始搜索:(NSString*)搜索字符串
{
if(self.localSearch.search)
{
[self.localSearch取消];
}
MKLocalSearchRequest*request=[[MKLocalSearchRequest alloc]init];
request.naturalLanguageQuery=searchString;
MKLocalSearchCompletionHandler completionHandler=^(MKLocalSearchResponse*响应,NSError*错误)
{
如果(错误!=nil)
{
NSString*errorStr=[[error userInfo]valueForKey:NSLocalizedDescriptionKey];
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“找不到位置”
信息:errorStr
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报显示];
}
其他的
{
self.places=[response-mapItems];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
};
if(self.localSearch!=nil)
{
self.localSearch=nil;
}
self.localSearch=[[MKLocalSearch alloc]initWithRequest:request];
[self.localSearch startWithCompletionHandler:completionHandler];
[UIApplication sharedApplication].networkActivityIndicatorVisible=是;
self.region=[self-setupgeofense:self.geofense.latitude.doubleValue:self.geofense.longitude.doubleValue];
}
-(无效)搜索栏搜索按钮选中:(UISearchBar*)搜索栏
{
[搜索栏辞职FirstResponder];
[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];
....
}
....
@结束

我建议在调用
self.places=[response-mapItems]之前记录
[response-mapItems]
self.places
。这很可能会让我们对形势有所了解:

NSLog(@"self.places is: %@", self.places);
NSLog(@"map items is: %@", [response mapItems]);

您的程序流程有一个问题:当您调用

[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:
方法分为“before”和“after”两部分。“之前”部分保持不变-它告诉
开始搜索:
启动搜索


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

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];
    ....
}