I am trying to create an NSMutableArray
of the ranges discovered from NSRegularExpression
, but I cannot get the NSMutableArray
to hold objects. Help?
Declare the array by: NSMutableArray *matches = [[NSMutableArray alloc]init];
At the end of my regular expression loops:
for (NSTextCheckingResult *aMatch in minedMatches) {
NSRange matchRange = [aMatch range];
[matches addObject: [NSValue valueWithRange:matchRange]];
}
In another part of my code, I have the a for loop wanting to use matches
; however, it is not full:
if (matches != nil) {
for (int i = 0; i < matches.count; i++) {
[attributedString addAttribute:NSForegroundColorAttributeName value: minedColor range:[[matches objectAtIndex:i]rangeValue]];
}
}
**Note:
minedColor
, minedMatches
and attributedString
are declared properly throughout my code. I am using addAttribute
in a separate location because I need to only change the color of the text in between sections of key words such as "Go" and "end".
**Edit 1 (request for entire method)
- (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:®exErrorMined];
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
}
I did not post the entire method originally because it requires a lot of explaining, as I'm sure you can see. Basically, the user will input text into a text view. That text is then data mined if the words fall between "Start" and "end". These key words signal triggers that change the value of [gCore dataMiningInProgress]
, which is a global object.
Currently, if a user were to type "Start the cat is outside end", the words "cat" and "outside" will change color when the user inputs "end". If the user inputs more string such as: "Start the cat is now inside end", the word "cat" will automatically turn green even before the user types "end". I want to prevent this from happening. I only want the color to change during the individual sections of "start......end"
All outside variables are in working order, the only thing I cannot get thus far is the addAttribute
from the array of ranges in matches
because although it does not say it is nil
, matches.count
is 0 in the else if()
conditional.
Using suggestions from @kambala and @LyricalPanda, my original problem of matches
being nil
in the else
statement was solved through a scoping issue. Although I created a property in the header file for matches
and @synthesize
'd it, my NSMutableArray
was not being written to on a class-level scale. I changed the scope to create a global variable for matches
that can now be accessed from any file. Seems like a waste of some coding power, but that was how I was able to get the MutableArray
to hold objects outside of one instance. Using the @extern
command, allows successful reading and writing of the array full of ranges.