我的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:@""];
}
}
答案 0 :(得分: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?如果是这样,只需更改这两行代码)然后继续执行以下{{ 1}} / if
。