0

这是我的方法

- (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 后第二次调用。如果我再次从 NSNotification 选择器调用此方法,新的 Web 数据将被写入旧数据之上,并吐出不一致的值:)

4

1 回答 1

2

将其设置为nil删除对该对象的引用。如果您正在使用 ARC 并且它是对该strong对象的最后引用,则系统会自动释放该对象并释放其内存。无论哪种情况,它都不会“将其清除并准备好重新使用”,您需要重新分配和初始化您的对象。如果您只想删除所有对象,并假设myLocationsToSort是一个,NSMutableArray您可以调用

[self.myLocationsToSort removeAllObjects];

否则你需要做

self.myLocationsToSort = nil;
self.myLocationsToSort = [[NSMutableArray alloc] init];
于 2013-04-17T15:39:12.403 回答