gpt4 book ai didi

objective-c - 如何检查通过脚本获取的字符串的值?

转载 作者:行者123 更新时间:2023-12-03 17:53:11 24 4
gpt4 key购买 nike

我的 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)。 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 个字符吗?如果是这样,只需更改这两行代码即可),然后继续执行以下 如果/else

关于objective-c - 如何检查通过脚本获取的字符串的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com