0

我试图创建从NSRegularExpression发现的范围的NSMutableArray,但我无法让NSMutableArray保存对象。帮帮我?创建一个NSMangeArray的NSRange's,然后正确读取范围值

声明由阵列:NSMutableArray *matches = [[NSMutableArray alloc]init];

在我的正则表达式的端环:

for (NSTextCheckingResult *aMatch in minedMatches) { 
    NSRange matchRange = [aMatch range]; 
    [matches addObject: [NSValue valueWithRange:matchRange]]; 
} 

在我的代码另一部分,我有循环想要使用matches的一个;然而,它不是全:

if (matches != nil) { 
      for (int i = 0; i < matches.count; i++) { 
       [attributedString addAttribute:NSForegroundColorAttributeName value: minedColor range:[[matches objectAtIndex:i]rangeValue]]; 
      } 
     } 

**注:

minedColorminedMatchesattributedString在整个我的代码申报正确。我在单独的位置使用addAttribute,因为我需要仅更改关键词部分(如“开始”和“结束”)之间的文本颜色。

**编辑1(对于整个方法请求)

- (void)textViewDidChange:(UITextView *)textView { 

self.notepadTextView.font = [UIFont fontWithName:@"ProximaNova-Regular" size:20]; //custom font 
UIFont *normalFont = [UIFont fontWithName:@"ProximaNova-Regular" size:20];//fail-safe font for attributed string 
NSString *textEntryContents = [[self notepadTextView ]text]; //declares user inputted string 
[gCore processSpeechText:textEntryContents]; //internal processing 
NSMutableArray *mined = [gCore getHighLightContainer]; //array with strings that need to be colored 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textEntryContents 
                        attributes:@{NSFontAttributeName: normalFont}]; //initialize attributed string 
matches = [[NSMutableArray alloc]init]; //initialize matches 
UIColor *minedColor = [UIColor colorWithRed:(126.0/255.0) green:(204.0/255.0) blue:(136.0/255.0) alpha:1.0]; //initialize color for attributed string 

BOOL colorChangeDidRun = '\0'; //initialize if color was changed 

if ([gCore dataMiningInProgress] == YES) { //if it is the start of a section 
    colorChangeDidRun = NO; 
    if (mined != nil){ //fail-safe 
     for (int i = 0; i < mined.count; i++){ 
      NSError *regexErrorMined; 
      NSRegularExpression *regexMined = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"%@",mined[i]] 
                         options:NSRegularExpressionCaseInsensitive error:&regexErrorMined]; 
      if (!regexErrorMined) { 
       NSArray *minedMatches = [regexMined matchesInString:[attributedString string] 
                  options:0 
                   range:NSMakeRange(0, [[attributedString string] length])]; 
       for (NSTextCheckingResult *aMatch in minedMatches) { 
        NSRange matchRange = [aMatch range]; 
        [matches addObject: [NSValue valueWithRange:matchRange]]; //add range values to matches array      
       } 
      } 
     } 

    } 

} 
else if ([gCore dataMiningInProgress] == NO) { //if end of section 
    if (colorChangeDidRun == NO) { //if the color change has not happened yet 
     if (matches != nil) { 
      for (int i = 0; i < matches.count; i++) { 
       colorChangeDidRun = YES; //prevent color change in unnecessary spots 
       [attributedString addAttribute:NSForegroundColorAttributeName value: minedColor range:[[matches objectAtIndex:i]rangeValue]];    
      } 
     } 
    } 
} 

self.notepadTextView.attributedText = attributedString; //output attributed string 

}

我没有最初发布整个方法,因为它需要大量的解释,因为我敢肯定,你可以看到。基本上,用户将文本输入到文本视图。如果的文字落在“开始”和“结束”之间,则该文本然后是数据开采。这些关键字信号触发器会改变[gCore dataMiningInProgress]的值,这是一个全局对象。

目前,如果用户输入“开始猫在户外”,当用户输入“结束”时,“猫”和“外部”这两个字将会改变颜色。如果用户输入更多的字符串,例如:“开始猫现在内部结束”,即使在用户键入“结束”之前,单词“猫”也会自动变为绿色。我想防止这种情况发生。我只希望在“开始......结束”的各个部分中更改颜色

所有外部变量都处于正常工作状态,我迄今为止唯一无法获得的是addAttribute因为尽管它不会说nil,matches.countelse if()有条件。

+0

定义“未满”。是匹配一个局部变量还是一个实例变量? – rmaddy

+0

你有没有证实你实际上获得有效的matchRange时添加?在[matches addObject:]上放置一个调试断点,然后查看对象数是否在 – LyricalPanda

+0

之后立即上升。另外,您确定实际运行第一个示例代码中的for循环,并将对象添加到匹配阵列? – timgcarlson

回答

0

使用@kambala和@LyricalPanda的建议,我的原始问题matcheselse声明中是nil通过范围问题解决。虽然我在头文件中为matches@synthesize'd创建了一个属性,但我的NSMutableArray没有在类级别上写入。我改变了范围,为matches创建一个全局变量,现在可以从任何文件访问它。似乎浪费了一些编码能力,但这就是我如何让MutableArray在一个实例之外保存对象。使用@extern命令可以成功读取和写入满量程的数组。

1

这里有一个非常基本的错误:无法一次性执行ifelse if这两个分支。所以如果[gCore dataMiningInProgress] == YES那么只有matches会填充对象,就这些。如果条件是NO,则matches是一个空数组(因为它没有明显填充对象)。

P.S.写if ([gCore dataMiningInProgress] == YES) ... else if ([gCore dataMiningInProgress] == NO)是没有用的,因为如果它没有评估到YES,那肯定是NO :)所以这只是一个if-else构造。

+0

我甚至没有想到'if-else'的小细节,所以非常感谢!对于如何访问'else'节中的'matches'而不是一个空数组,你有什么建议?我试图在我的.h文件中声明'matches',然后合成它,但是这也不起作用:/ – momodude22

+1

@ momodude22这真的取决于您的代码流。你想要匹配每个textDidChange持续吗?然后将其添加为属性,并在退出textView时将viewDidLoad&removeAllObjects初始化为数组。如果你不想让它坚持下去,那么放下if/else。 – LyricalPanda

+0

我已经成功地改变了单词的颜色,而没有使用if-else的东西,但是这里的问题是,一旦用户输入“end”,就会发生初始颜色变化。如果他们在此之后通过键入“开始”开始一个新的部分,则在用户键入“end”之前,如果用户在前一部分中输入了该字,则会发生颜色变化。我不希望它自动更改颜色,如果它曾经被看到过,我希望它只在用户输入“end”时才改变颜色,这就是为什么我相信需要if-else语句的原因@LyricalPanda – momodude22