2013-04-17 47 views
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已经是空。我认为将价值设定为零基本上已经清理出来,准备好重新使用了?我需要这样做,因为此方法在启动时调用一次,并且在从Web获取数据时收到NSNotification后第二次。如果我再次从NSNotification选择器调用此方法,新的Web数据将被写入旧数据的顶部,并且它会产生不一致的值::)

回答

2

将其设置为nil将删除对该对象的引用。如果您正在使用ARC并且它是对该对象的最后一个strong引用,则系统会自动释放该对象并释放其内存。在任何情况下,它都不会“清理掉并准备好重新使用”,您需要重新分配和初始化对象。如果你宁愿只是删除所有的对象,并假设myLocationsToSortNSMutableArray你可以叫

[self.myLocationsToSort removeAllObjects]; 

否则,你需要做的

self.myLocationsToSort = nil; 
self.myLocationsToSort = [[NSMutableArray alloc] init]; 
+0

的“垃圾收集”的一提的是错在这里。 ARC不以任何方式涉及垃圾收集。 –

+0

@KenThomases它是什么?只需在refcount = 0时自动删除对象? –

+0

是的,它是参考计数的内存管理。 ARC只是自动化保留和发布。 –

相关问题