0

NSMutableArrayから検出された範囲のを作成しようとしていますが、にオブジェクトを保持さNSRegularExpressionせることができません。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]]; 
            }
        }

**ノート:

minedColorminedMatchesおよびattributedStringコード全体で適切に宣言されています。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]は、グローバル オブジェクトである の値を変更するトリガーを通知します。

現在、ユーザーが「Start the cat is outside end」と入力すると、ユーザーが「end」と入力すると、「cat」と「outside」という単語の色が変わります。ユーザーが「Start the cat is now inside end」などの文字列をさらに入力すると、ユーザーが「end」と入力する前であっても、「cat」という単語が自動的に緑色に変わります。これを未然に防ぎたい。「開始......終了」の個々のセクションでのみ色を変更したい

すべての外部変数は正常に機能しています。これまでのところ取得できないのは、範囲のaddAttribute配列からのものだけです。matchesnilmatches.countelse if()

4

2 に答える 2

0

@kambala と @LyricalPanda からの提案を使用して、ステートメントに含まれてmatchesいるという私の最初の問題は、スコープの問題によって解決されました。ヘッダー ファイルにプロパティを作成してそれを作成しましたが、クラス レベルのスケールでは書き込まれませんでした。スコープを変更して、任意のファイルからアクセスできるグローバル変数を作成しました。コーディング能力をいくらか無駄にしているように見えますが、それがに 1 つのインスタンスの外にオブジェクトを保持させることができた方法です。このコマンドを使用すると、範囲がいっぱいの配列の読み取りと書き込みを正常に行うことができます。nilelsematches@synthesizeNSMutableArraymatchesMutableArray@extern

于 2014-07-10T15:42:31.550 に答える