2013-04-17 44 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只是自動化保留和發佈。 –

相關問題