这是我的方法
- (void)populateLocationsToSort {
//1. Get UserLocation based on mapview
self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];
//Set self.annotationsToSort so any new values get written onto a clean array
self.myLocationsToSort = nil;
// Loop thru dictionary-->Create allocations --> But dont plot
for (Holiday * holidayObject in self.farSiman) {
// 3. Unload objects values into locals
NSString * latitude = holidayObject.latitude;
NSString * longitude = holidayObject.longitude;
NSString * storeDescription = holidayObject.name;
NSString * address = holidayObject.address;
// 4. Create MyLocation object based on locals gotten from Custom Object
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];
// 5. Calculate distance between locations & uL
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:self.userLocation];
annotation.distance = calculatedDistance/1000;
//Add annotation to local NSMArray
[self.myLocationsToSort addObject:annotation];
**NSLog(@"self.myLocationsToSort in someEarlyMethod is %@",self.myLocationsToSort);**
}
//2. Set appDelegate userLocation
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
myDelegate.userLocation = self.userLocation;
//3. Set appDelegate mylocations
myDelegate.annotationsToSort = self.myLocationsToSort;
}
在粗体行中,self.myLocationsToSort已为空。我认为将值设置为nil基本上是清理它,准备重新使用?我需要这样做,因为这个方法在启动时调用一次,在收到NSNotification后第二次从Web获取数据时调用。如果我再次从NSNotification选择器调用此方法,则新的Web数据将写入旧数据之上,并且会发出一堆不一致的值:)
答案 0 :(得分:2)
将其设置为nil
会删除对该对象的引用。如果您使用ARC并且它是该对象的最后一个strong
引用,则系统会自动释放该对象并释放其内存。在任何一种情况下,它都不会“清理它并准备好重复使用”,你需要重新分配和初始化你的对象。如果您只想删除所有对象,并假设myLocationsToSort
是NSMutableArray
,则可以调用
[self.myLocationsToSort removeAllObjects];
否则你需要做
self.myLocationsToSort = nil;
self.myLocationsToSort = [[NSMutableArray alloc] init];