2014-01-24 4 views
0

배열의 색인 생성에 심각한 문제가 있습니다. 나는 2 일 동안이 일을하고 있었고 아직 답을 찾을 수 없었다.두 문자열 간의 문자열 바꾸기

배열에서 특정 문자를 검색 한 다음 다른 문자열로 바꾸고 싶습니다. replaceObjectAtIndex 메서드를 사용하고 있지만 내 코드가 작동하지 않습니다.

내 코드는 다음과 같습니다.

NSString *commentText = commentTextView.text; 
    NSUInteger textLength = [commentText length]; 
    NSString *atSign = @"@"; 
    NSMutableArray *commentArray = [[NSMutableArray alloc] init]; 
    [commentArray addObject:commentText]; 

    for (int arrayCounter=1; arrayCounter<=textLength; arrayCounter++) 
    { 
     NSRange isRange = [commentText rangeOfString:atSign options:NSCaseInsensitiveSearch]; 
     if(isRange.location != NSNotFound) 
     { 
      commentText = [commentText stringByReplacingOccurrencesOfString:commentText withString:atSign]; 
      [_mentionsearch filtrele:_mentionText]; 
      id<textSearchProtocol> delegate; 
      [delegate closeList:[[self.searchResult valueForKey:@"user_name"] objectAtIndex:indexPath.row]]; 
     } 
    } 

좋아, 이제 텍스트에서 "@"기호를 찾을 수 있으며 일치시킬 수 있습니다. 그러나 이것은 어떤 문자열도 "@"기호로 대체 할 수없는 문제의 근원입니다. 다음은 코드의 마지막 부분입니다.

-(void)closeList 
    { 
     NSArray *arrayWithSign = [commentTextView.text componentsSeparatedByString:@" "]; 
     NSMutableArray *arrayCopy = [arrayWithSign mutableCopy]; 
     [arrayCopy replaceObjectAtIndex:isRange.location withObject:[NSString stringWithFormat:@"@%@",username]]; 
    } 

메신저 로깅 isRange.location 값, 그것은 정확한 돌아 오면. 그러나 메신저를 실행하려고하면 응용 프로그램이 충돌합니다. 그래서, 나는 대체 할 수 없다 [NSString stringWithFormat : @ "@ % @", username] 매개 변수. 이 문제를 어떻게 해결할 수 있습니까?

+1

어떻게 부숴 지나요? 오류 로그를 포함시켜야합니다. – jrturton

+0

문자열 검색에서 가져온 범위를 배열의 인덱스로 사용하려는 이유는 무엇입니까? – Wain

+0

로그의 단점은 없습니다. 내가 가지고있는 것, (lldb) 0x6ae8952 : jae 0x6ae8962; __pthread_kill + 26. –

답변

2

올바르게 이해하면 문자열의 하위 문자열을 새 문자열로 변경하려고합니다. 이 경우 NSString의 stringByReplacingOccurrencesOfString 메서드를 직접 사용하지 않는 것이 좋습니다.

NSString *stringToBeChanged = @"..."; 
NSString *stringToBeChangedWith = @"..."; 
NSString *commentTextNew = [commentText stringByReplacingOccurrencesOfString:stringToBeChanged withString:stringToBeChangedWith]; 
+0

배열 결과에 대해 큰 실수를 했어. 그건 솔루션을 쓰입니다 :) –

+1

"<>"사이의 문자열을 변경하고 싶습니다 에서 < new tring> 내 문자열은 동적 문자열입니다 그럼 어떻게 문자열을 식별 할 수 있습니다 –

관련 문제