繁体   English   中英

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

[英]Create an NSMutableArray of NSRange's and properly read the range values later

我正在尝试创建从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”和“ 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 = '\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]值的触发器,该值是一个全局对象。

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

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

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

PS这是没有用书面形式if ([gCore dataMiningInProgress] == YES) ... else if ([gCore dataMiningInProgress] == NO)因为如果它不评估为YES ,那么它肯定NO :)所以它只是一个if-else结构。

使用@kambala和@LyricalPanda的建议,我的else语句中最初的matches问题为nil ,这是通过范围界定问题解决的。 尽管我在头文件中为matches创建了一个属性,并对其进行了@synthesize的创建,但我的NSMutableArray并未在类级别上被写入。 我更改了范围,以创建一个matches的全局变量,该变量现在可以从任何文件访问。 似乎是在浪费一些编码能力,但这是我使MutableArray能够保存一个实例之外的对象的方式。 使用@extern命令,可以成功读取和写入充满范围的数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM