我試圖創建從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]];
}
}
**注:
minedColor
,minedMatches
和attributedString
在整個我的代碼申報正確。我在單獨的位置使用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:®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
}
我沒有最初發布整個方法,因爲它需要大量的解釋,因爲我敢肯定,你可以看到。基本上,用戶將文本輸入到文本視圖。如果的文字落在「開始」和「結束」之間,則該文本然後是數據開採。這些關鍵字信號觸發器會改變[gCore dataMiningInProgress]
的值,這是一個全局對象。
目前,如果用戶輸入「開始貓在戶外」,當用戶輸入「結束」時,「貓」和「外部」這兩個字將會改變顏色。如果用戶輸入更多的字符串,例如:「開始貓現在內部結束」,即使在用戶鍵入「結束」之前,單詞「貓」也會自動變爲綠色。我想防止這種情況發生。我只希望在「開始......結束」的各個部分中更改顏色
所有外部變量都處於正常工作狀態,我迄今爲止唯一無法獲得的是addAttribute
因爲儘管它不會說nil
,matches.count
在else if()
有條件。
定義「未滿」。是匹配一個局部變量還是一個實例變量? – rmaddy
你有沒有證實你實際上獲得有效的matchRange時添加?在[matches addObject:]上放置一個調試斷點,然後查看對象數是否在 – LyricalPanda
之後立即上升。另外,您確定實際運行第一個示例代碼中的for循環,並將對象添加到匹配陣列? – timgcarlson