0

我的 Mac 应用程序通过脚本从另一个应用程序获取 2 个字符串值。在某些条件下,发送方提供“0-1”。我需要检测到这一点并将显示它的文本框清空。以下仅显示第二个字符串的代码,可在调试器中运行,但在其外部运行时无效。

- (void)controlTextDidChange:(NSNotification *)notification
{
// there was a change in a text control
int tmpInt2 = 0;
NSMutableString *tmp2 = [NSMutableString stringWithString:[inputTextField2 stringValue]];
//NSLog(@"text box changed. value: %i", val);
if ([tmp2 length] > 3)
{
    tmp2 = [NSMutableString stringWithString:[tmp2 substringToIndex:[tmp2 length] - 1]];
    [inputTextField2 setStringValue:tmp2];
}
if ([tmp2 length] == 3)
{
    tmpInt2 = [tmp2 intValue];
    if (tmpInt2 > 360 || tmpInt2 < 0 || [tmp2 isEqualToString:@"0-1"])
    {
        //[self showAlert:@"Heading must be between 000 and 360"];
        [inputTextField2 setStringValue:@""];
        //[inputTextField2 setBackgroundColor:[NSColor yellowColor]];
        [tmp2 setString:@""];
    }
}
if ([[inputTextField2 stringValue] rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
{
    NSLog(@"This is not a positive integer");
    //NSMutableString *strippedString = [NSMutableString stringWithCapacity:tmp.length];
    [inputTextField2 setStringValue:@""];
    //[[inputTextField2 cell] setBackgroundColor:[NSColor yellowColor]];
    [tmp2 setString:@""];
}
/*
if ([tmp2 isEqualToString:@"0-1"])
{
    [inputTextField2 setStringValue:@""];
    [tmp2 setString:@""];
}
 */
if ([tmp2 rangeOfString:@"-"].location == NSNotFound) {
    NSLog(@"string does not contain 0-1");
} else {
    NSLog(@"string contains 0-1!");
    [inputTextField2 setStringValue:@""];
    [tmp2 setString:@""];
}

}

4

1 回答 1

1

您应该查看@trojanfoe 关于使用NSFormatter或其预定义子类之一的建议。但是,您似乎误解了 的目的NSMutableString,因此我提供了以下版本的代码,其中嵌入了一些注释。用于测试的文本字段被赋予了一个占位符值“输入标题”,并且假设 ARC 已启用。使用现代属性访问语法 (object.property)。HTH。

- (void)controlTextDidChange:(NSNotification *)notification
{
   // there was a change in a text control

   NSTextField *inputTextField = notification.object;   // get the field
   NSTextFieldCell *fieldCell = inputTextField.cell;    // and its cell - we use the placeholder text for feedback in this sample

   fieldCell.placeholderString = @"Enter heading";      // user has typed, restore default message

   NSString *contents = inputTextField.stringValue;     // an NSMutableString is not required, you never mutate this string
   NSUInteger length = contents.length;

   if (length > 3)
   {
      // remove last character - did you mean to truncate to three characters?
      inputTextField.stringValue = [contents substringToIndex:length - 1];
   }
   else if (length == 3)
   {
      int tmpInt = contents.intValue;
      if (tmpInt > 360 || tmpInt < 0 || [contents isEqualToString:@"0-1"])
      {
         fieldCell.placeholderString = @"Heading must be between 000 and 360"; // inform user why field was blanked             
         inputTextField.stringValue = @"";
      }
   }
   else if ([contents rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
   {
      // you might want different logic here
      // if a user types "12Y" you delete everything, deleting just the "Y" might be more friendly
      // ("Y" picked as an example as it could be a miss hit for the 6 or 7 keys)

      fieldCell.placeholderString = @"Enter a positive integer"; // inform user why field was blanked    
      inputTextField.stringValue = @"";
   }
}

附录 - 评论跟进

究竟您期望什么输入以及您希望如何处理这些输入尚不清楚。第一个if只是从长度超过 3 的字符串中删除最后一个字符,而不进行任何其他检查。但是我可能在这里误解了您的意图,您本来打算在此之后继续处理if,例如:

...
   if (length > 3)
   {
      // remove last character - did you mean to truncate to three characters?
      contents = [contents substringToIndex:length - 1];
      length -= 1;
   }

   if (length == 3)
   {
      ...

这意味着如果您的输入长于 3 个字符,则删除最后一个字符(您不想简单地截断为 3?如果是这样,只需更改这两行代码即可),然后继续执行以下if/ else

于 2013-10-21T19:51:54.020 回答