Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何检查通过脚本获取的字符串的值?_Objective C_Cocoa - Fatal编程技术网

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

Objective c 如何检查通过脚本获取的字符串的值?,objective-c,cocoa,Objective C,Cocoa,我的Mac应用程序通过脚本从另一个应用程序获取2个字符串值。在某些情况下,发送方提供“0-1”。我需要检测到这一点,并清空显示它的文本框。以下仅显示第二个字符串的代码,在调试器中工作,但在调试器外部运行时不工作 - (void)controlTextDidChange:(NSNotification *)notification { // there was a change in a text control int tmpInt2 = 0; NSMutableString *tmp2 = [

我的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:@""];
}
-(void)controlTextDidChange:(NSNotification*)通知
{
//文本控件中发生了更改
int tmpInt2=0;
NSMutableString*tmp2=[NSMutableString stringWithString:[inputTextField2 stringValue]];
//NSLog(@“文本框已更改。值:%i”,val);
如果([tmp2长度]>3)
{
tmp2=[nsmutablestringwithstring:[tmp2 substringToIndex:[tmp2 length]-1]];
[InputExtField2设置字符串值:tmp2];
}
如果([tmp2长度]==3)
{
tmpInt2=[tmp2 intValue];
如果(tmpInt2>360 | | tmpInt2<0 | |[tmp2等质量字符串:@“0-1”]))
{
//[自显示警报:@“标题必须介于000和360之间”];
[InputExtField2 SetString值:@”“;
//[InputExtField2 setBackgroundColor:[NSColor yellowColor]];
[tmp2设置字符串:@”“;
}
}
if([[inputTextField2 stringValue]rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet]InversedSet]].location!=NSNotFound)
{
NSLog(@“这不是一个正整数”);
//NSMutableString*strippedString=[NSMutableString stringWithCapacity:tmp.length];
[InputExtField2 SetString值:@”“;
//[[inputTextField2单元格]setBackgroundColor:[NSColor yellowColor]];
[tmp2设置字符串:@”“;
}
/*
if([tmp2 IsequalString:@“0-1”])
{
[InputExtField2 SetString值:@”“;
[tmp2设置字符串:@”“;
}
*/
if([tmp2 rangeOfString:@“-”]。位置==NSNotFound){
NSLog(@“字符串不包含0-1”);
}否则{
NSLog(@“字符串包含0-1!”);
[InputExtField2 SetString值:@”“;
[tmp2设置字符串:@”“;
}

}

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


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

我认为可以通过在
NSFormatter
子类中实现代码,然后将格式化程序与文本字段关联来完成所有这些。我不确定在更改回调中对文本字段进行更改是否是一个好主意,尤其是当格式化程序提供这种管道时。我正在研究它。谢谢你的建议。你的代码解决了这个问题,但我不知道为什么,我正在努力解决这个问题。NSFormatter建议是研究清单上的另一项。来自Delphi的背景,我喜欢“object.property”语法。抱歉,我说得太早了。我仍在显示“0-1”。我怀疑您输入的内容类似于
0-1
,后跟一个换行符。我已经添加了一个附录,但这可能仍然不是您想要做的,例如,
0-1xy
将由第二个
if
else
分支处理,因此将生成与
0-1x
不同的消息。只有你知道你需要处理什么输入以及你需要对它们做什么。通过Applescript为我提供信息的程序计算一个指向某个国家的3位数标题,如果有数据,则计算指向该国特定位置的3位数标题。如果位置数据不可用于计算,则会发出“0-1”。或者至少这就是我的文本框中显示的内容。我来检查一下长度,看看是不是长度问题。两个标题几乎同时出现。由于代码在调试器中工作,我开始怀疑时间问题。继续!在进行任何处理之前,我将这两个字段的捕获移到了方法的顶部。这似乎已经做到了。
- (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 (length > 3)
   {
      // remove last character - did you mean to truncate to three characters?
      contents = [contents substringToIndex:length - 1];
      length -= 1;
   }

   if (length == 3)
   {
      ...