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