NSError不会清除旧错误

时间:2012-07-05 05:09:58

标签: objective-c cocoa

长时间听众,第一次打电话给我,如果我不够明确,请原谅我。我目前正在摸索NSError对象的问题。以下是设置方式:

-(void)mainMethod {
   NSError *myError = nil;
   NSString *myString = @"A really great string. 1234.";
   if ([self parseAndStoreString:myString error:&myError] == NO) {
      [self popupWithErrorMessage:[[myError localizedDescription] copy]];
   }

-(BOOL)parseAndStoreString:(NSString *)incomingString error:(NSError *__autoreleasing *)err {
   if ([self setupSomething error:err] == NO) {
      return NO;
   }
   if ([self doSomethingWithString:incomingString error:err] == NO) {
      return NO;
   }
   if ([self storeString:incomingString error:err] == NO) {
      return NO;
   }
}

-(BOOL)doSomethingWithString:(NSString *)incomingString error:(NSError *__autoreleasing *)err {
   if (incomingString == nil) {
     DLog(@"String was null! See? : %@", incomingString);
     NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:EDEmptyStringErrorDescription forKey:NSLocalizedDescriptionKey];
     if (err != nil) *err = [NSError errorWithDomain:EDStringDomain code:EDEmptyStringError userInfo:errorInfo];
     return NO;
   }
   if (somethingElseIsWrongWithString) {
     DLog(@"Another Problem Geesh");
     NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:EDAnotherStringErrorDescription forKey:NSLocalizedDescriptionKey];
     if (err != nil) *err = [NSError errorWithDomain:EDStringDomain code:EDAnotherStringError userInfo:errorInfo];
     return NO;
   }
}

现在,无论哪个错误命中正确建立并在弹出窗口中按预期显示第一次。当用户点击再次激活'mainMethod'的东西或另一个调用'parseAndStoreString:error:'的方法(有三个或四个调用该方法)时,会出现问题。如果还有其他错误,则弹出窗口显示第一个错误的描述,第二个错误粘在一起形成一个字符串。这是ARC代码,所以我的理解是编译器应该在出现错误后释放myError对象(或其他方法正在创建的任何NSError对象)。我不认为问题出在'popupWithErrorMessage'方法中,因为我只传递了本地化描述的副本,该副本会被[popupView orderOut:self]显示然后销毁。关于如何将这些错误消息堆积在NSError对象中的任何想法?

@ torrey.lyons:当然,这是togglePopupWithMessage的代码:onView -

- (void)popupWithErrorMessage:(NSString *)errorToDisplay {
   if (!messagePopup) {
      if (errorToDisplay == nil) {
         DLog(@"Warning, incoming error message was nil");
         return;
      }

      NSDictionary *messageAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSColor whiteColor], NSForegroundColorAttributeName,
                                     [NSFont systemFontOfSize:12], NSFontAttributeName,
                                     nil];
      NSAttributedString *messageWithAttributes = [[NSAttributedString alloc]
                                                  initWithString:errorToDisplay
                                                  attributes:messageAttributes];

      NSPoint messagePoint = NSMakePoint(NSMidX([[self.mainSplitView.subviews objectAtIndex:1] frame]),
                                        NSMinY([anchorView frame])+4.0f);
      messagePopup = [[MAAttachedWindow alloc] initWithView:popupView 
                                            attachedToPoint:messagePoint 
                                                   inWindow:[self window]
                                                     onSide:MAPositionBottom 
                                                 atDistance:0];
      [messagePopup setAlphaValue:0.0f];

      NSTextStorage *messageStorage = popupMessage.textStorage;
      [messageStorage beginEditing];
      [messageStorage insertAttributedString:messageWithAttributes atIndex:0];
      [messageStorage endEditing];

      [messagePopup setFrame:NSMakeRect(messagePopup.frame.origin.x, messagePopup.frame.origin.y, 250.0f, 100.0f)
                   display:YES];

      messageWithAttributes = nil;
      messageAttributes = nil;
      errorToDisplay = @"";

      [[self window] addChildWindow:messagePopup ordered:NSWindowAbove];


      [[messagePopup animator] setAlphaValue:1.0f];
      [popupMessage setNeedsDisplay:YES];

      [NSTimer scheduledTimerWithTimeInterval:3.5
                                       target:self
                                     selector:@selector(turnOffPopupFromTimer:)
                                     userInfo:nil
                                      repeats:NO];
   } else {
      [[self window] removeChildWindow:messagePopup];
      [messagePopup fadeOutAndOrderOut:YES];
      messagePopup = nil;
   }
- (void)turnOffPopupFromTimer:(NSTimer *)timer {
   if (messagePopup) {              
                                    //Added to correct problem \/\/\/\/
      NSTextStorage *messageStorage = popupMessage.textStorage;
      [messageStorage beginEditing];
      [messageStorage deleteCharactersInRange:NSMakeRange(0, messageStorage.characters.count)];
      [messageStorage endEditing];  
                                    //Added to correct problem /\/\/\/\
      [[self window] removeChildWindow:messagePopup];
      [messagePopup fadeOutAndOrderOut:YES];
      messagePopup = nil;
   }
}

1 个答案:

答案 0 :(得分:1)

我怀疑问题出在popupWithErrorMessage,这是堆积本地化的描述。即使NSError对象的生命周期中存在错误,为单独的错误创建的每个错误都是一个单独的对象,并且不会将其他NSError对象的本地化描述填充到其中。另一方面,您对popupWithErrorMessage的描述听起来很可疑:[popupView orderOut:self]只是从屏幕上移除窗口,但不释放它或导致它被销毁。您可以发布popupWithErrorMessage的代码吗?