0

长期听众,第一次来这里,如果我不够明确,请原谅我。我目前正在为 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;
   }
}
4

1 回答 1

1

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

于 2012-07-05T06:59:39.190 回答