如何检查通过脚本获取的字符串的值



我的 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:@""];
}

}

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

- (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

最新更新