对象在传入方法后被取消分配

时间:2011-12-21 14:29:23

标签: objective-c ios4

我有一个应用程序,当我将对象传递给方法时,有时对象的值会变为null。我在博客中找不到这样的东西,请建议我应该做些什么。我觉得这就像通过参考或价值传递,但不是很了解。

变量在* .h文件中声明,如

     NSNumber* resultSelectedWeekValue;

和财产

     @property (nonatomic, retain) NSNumber* resultSelectedWeekValue;

和羞辱

    @synthesize resultSelectedWeekValue =       _resultSelectedWeekValue;

这是一个从一个popover

获取值的委托方法
// Show discription of Selected week
         -(void) getSelectedWeekDelegateMethod:(NSString*) getSelectedWeek {

        _resultSelectedWeekValue = [_dbObject getSelectedWeekValue:getSelectedWeek];

        NSLog(@"This is _tempOzs %@ ", _resultSelectedWeekValue);

        if ([_productAttributePopover isPopoverVisible]) {

          [_productAttributePopover dismissPopoverAnimated:YES];
       }
  }

      NSLog(@"This is _resultSelectedWeekValue %@ and class %@", _resultSelectedWeekValue, [_resultSelectedWeekValue class]);  // This say the object is deallocate? this is the code below

if ((_resultSelectedWeekValue != nil) && (_tempGranularProduct != nil || _tempLiquidProduct != nil) && ( _tempLbs != nil || _tempOzs != nil)) {

    if ([_tempLbs intValue] != 0) {

        NSDate* _createdDate = [NSDate date];
        //Create the dateformatter object
        NSDateFormatter* _formatter = [[[NSDateFormatter alloc] init] autorelease];
        //Set the required date format
        [_formatter setDateFormat:@"yyyy-MM-dd"];
        //Get the string date
        NSString* _formatDate = [_formatter stringFromDate:_createdDate];
        NSNumber* _getNumBags = [self calculateGranularBags];
        NSNumber* _getProductID = [[_dbObject getAppliedProductWeightAttribute:_tempGranularProduct] objectForKey:@"productid"];
        NSNumber* _totalInvestment = [[NSNumber alloc] initWithInt:0];


        NSLog(@"This is _tempScenarioId %@ and class %@", _tempScenarioId, [_tempScenarioId class]);
        NSLog(@"This is _resultSelectedWeekValue %@ and class %@", _resultSelectedWeekValue, [_resultSelectedWeekValue class]);
        NSLog(@"This is _getProductID %@ and class %@", _getProductID, [_getProductID class]);
        NSLog(@"This is _tempLbs %@ and class %@", _tempLbs, [_tempLbs class]);
        NSLog(@"This is _getNumBags %@ and class %@", _getNumBags, [_getNumBags class]);
        NSLog(@"This is _totalInvestment %@ and class %@", _totalInvestment, [_totalInvestment class]);
        NSLog(@"This is _tempCarryover %@ and class %@", _tempCarryover, [_tempCarryover class]);



        NSMutableDictionary* _scenarioProductAttribute = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                  _tempScenarioId, @"scenarioid",
                                                  _resultSelectedWeekValue, @"weekvalue",
                                                  _getProductID, @"productid",
                                                  _tempLbs, @"lbper1000ft",
                                                  _getNumBags, @"numberofbags",
                                                  _totalInvestment, @"totalinvestment",
                                                  _tempCarryover, @"carry_n",
                                                  _formatDate, @"date",
                                                  nil];
        [_totalInvestment release];

        _tempScenarioId = [_dbObject insertScenarioProductAttribute:_scenarioProductAttribute andScenarioId:_tempScenarioId andProductId:_getProductID andSelectedWeekValue:_resultSelectedWeekValue];

        // Check Point
        [TestFlight passCheckpoint:@"INSERT_GRANULAR_RECORD"];
    } 

    if ([_tempOzs intValue] != 0) {

        NSDate* _createdDate = [NSDate date];
        //Create the dateformatter object
        NSDateFormatter* _formatter = [[[NSDateFormatter alloc] init] autorelease];
        //Set the required date format
        [_formatter setDateFormat:@"yyyy-MM-dd"];
        //Get the string date
        NSString* _formatDate = [_formatter stringFromDate:_createdDate];
        NSNumber* _getGallons = [self calculateLiquidGallons];
        NSNumber* _getProductID = [[_dbObject getAppliedProductWeightAttribute:_tempLiquidProduct] objectForKey:@"productid"];
        NSNumber* _totalInvestment = [[NSNumber alloc] initWithInt:0];


        NSLog(@"This is _tempScenarioId %@ and class %@", _tempScenarioId, [_tempScenarioId class]);
        NSLog(@"This is _resultSelectedWeekValue %@ and class %@", _resultSelectedWeekValue, [_resultSelectedWeekValue class]);  // This say the object is deallocate?
        NSLog(@"This is _getProductID %@ and class %@", _getProductID, [_getProductID class]);
        NSLog(@"This is _tempOzs %@ and class %@", _tempOzs, [_tempOzs class]);
        NSLog(@"This is _getGallons %@ and class %@", _getGallons, [_getGallons class]);
        NSLog(@"This is _totalInvestment %@ and class %@", _totalInvestment, [_totalInvestment class]);
        NSLog(@"This is _tempCarryover %@ and class %@", _tempCarryover, [_tempCarryover class]);



        NSMutableDictionary* _scenarioProductAttribute = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                          _tempScenarioId, @"scenarioid",
                                                          _resultSelectedWeekValue, @"weekvalue",
                                                          _getProductID, @"productid",
                                                          _tempOzs, @"ounces",
                                                          _getGallons, @"gallons",
                                                          _totalInvestment, @"totalinvestment",
                                                          _tempCarryover, @"carry_n",
                                                          _formatDate, @"date",
                                                          nil];
        [_totalInvestment release];
        _tempScenarioId = [_dbObject insertScenarioProductAttribute:_scenarioProductAttribute andScenarioId:_tempScenarioId      andProductId:_getProductID andSelectedWeekValue:_resultSelectedWeekValue];

1 个答案:

答案 0 :(得分:0)

将行更改为:

self._resultSelectedWeekValue = [_dbObject getSelectedWeekValue:getSelectedWeek];

如果您遇到“_resultSelectedWeekValue”变量值的问题。