创建一个 NSMutableArray of NSRange,并在以后正确读取范围值



我正在尝试创建从NSRegularExpression发现的范围的NSMutableArray,但我无法使NSMutableArray容纳对象。帮助

通过以下方式声明数组:NSMutableArray *matches = [[NSMutableArray alloc]init];

在我的正则表达式循环的末尾:

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

在我代码的另一部分中,我有一个for循环想要使用matches;然而,它还没有满:

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

**注:

minedColorminedMatchesattributedString在我的代码中被正确地声明。我在一个单独的位置使用addAttribute,因为我只需要更改关键字(如"Go"one_answers"end"(之间的文本颜色。

**编辑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 = ''; //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

}

我最初没有发布整个方法,因为它需要大量的解释,我相信你可以看到。基本上,用户会将文本输入到文本视图中。如果单词位于"开始"one_answers"结束"之间,则对该文本进行数据挖掘。这些关键字信号触发改变作为全局对象的[gCore dataMiningInProgress]的值。

目前,如果用户输入"Start the cat is outside end",当用户输入"end"时,单词"cat"one_answers"outside"将改变颜色。如果用户输入更多的字符串,例如:"Start the cat is now in end",即使在用户键入"end"之前,单词"cat"也会自动变为绿色。我想防止这种情况发生。我只想在"开始……结束"的各个部分更改颜色

所有外部变量都处于工作状态,到目前为止,我唯一无法获得的是matches中范围数组中的addAttribute,因为尽管它没有说它是nil,但matches.countelse if()条件中为0

这里有一个非常基本的错误:不可能一次执行ifelse if的两个分支。因此,如果[gCore dataMiningInProgress] == YES,那么只有matches会被对象填充,仅此而已。如果条件是NO,那么matches是一个空数组(因为它显然没有填充对象(。

附言:写if ([gCore dataMiningInProgress] == YES) ... else if ([gCore dataMiningInProgress] == NO)没有用,因为如果它不计算为YES,那么它肯定是NO:(所以它只是一个if-else构造。

使用@kambala和@LyricalPanda的建议,我最初的matcheselse语句中是nil的问题通过作用域问题得到了解决。尽管我在头文件中为matches@synthesize创建了一个属性,但我的NSMutableArray并没有按类级别写入。我更改了作用域,为matches创建了一个全局变量,现在可以从任何文件访问该变量。这似乎是在浪费一些编码能力,但这就是我如何让MutableArray保存一个实例之外的对象。使用@extern命令,可以成功读取和写入满量程的阵列。

最新更新