2010-04-23 3 views
4

나는 thsi 함수를 사용하고 예를 들어 해당 문자를 표시하기 위해 5를 더하여 문자열에서 다른 문자를 가져옵니다. 'a'는 'f'를 표시하고 'h'는 'm'을 표시합니다 .. 하지만 문제는이 문자를 'fm'과 같이 표시 할 때 사용할 수있는 문자열에 추가 할 수 없다는 것입니다 ... 누구 도움? heres the code strResult (mutablestring)가 null 만되고있다. 모든NSString에 하나씩 다른 문자를 추가 하시겠습니까?

[email protected]"John"; 

int a=[str length]; 

for(i=0;i<a;i++) 
{ 
    char ch=[str characterAtIndex:i]; 
    ch=ch+5; 
    temp=[NSString stringWithFormat:@"%c",ch]; 
    [strResult appendString:temp]; 
    NSLog(@"%c",ch); 
} 
+1

strResult가 정의하는 방법, 당신이 어딘가에를 초기화 한 당신이 다음 원하는 것은

? – Vladimir

답변

17

먼저, 당신은 당신과 같이 문자열 strResult을 할당 있는지 확인해야합니다 : 두 번째

NSMutableString *strResult = [NSMutableString string]; 

; 문자열에 문자를 추가하는 데 실제로 -appendFormat:을 사용할 수 있습니다. 임시 여분 문자열은 꽤 쓸모가 없습니다.

NSString *str = @"abcdef"; 
NSMutableString *strResult = [NSMutableString string]; 

for (NSUInteger i = 0; i < [str length]; i++) { 
    char ch = [str characterAtIndex:i] + 5; 
    NSLog(@"%c", ch); 
    [strResult appendFormat:@"%c", ch]; 
} 
NSLog(@"%@", strResult); 

이 생산한다 :

f 
g 
h 
i 
j 
k 
fghijk 
관련 문제