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