2011-05-14 9 views
-1

내 코드가 잘 될 때까지 잘 작동하며, 12와 같이 텍스트 필드에 두 자리 숫자를 입력하면 nslog는 1과 2 같은 2 자리 숫자를 반환합니다. 이제이 2 개의 한자리 숫자를 2 개의 문자열에 넣어야합니다. 누군가 나를 도울 수 있어요. 미리 감사드립니다.문자열을 문자로 변환

NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]]; 
NSArray *components = [depositOverTotalRwy 
         componentsSeparatedByString:@"/"]; 
NSString *firstThird = [components objectAtIndex:0]; 
    for(int i = 0; i < [firstThird length]; i++) 
{ 
    char extractedChar = [firstThird characterAtIndex:i]; 
    NSLog(@"%c", extractedChar); 
} 

답변

8

-stringWithFormat:을 사용할 수 있습니다.

NSString *s = [NSString stringWithFormat:@"%c", extractedChar]; 

편집 :

당신은 그 배열에 저장할 수 있습니다.

NSMutableArray *digits = [NSMutableArray array]; 

for (int i = 0; i < [s length]; i++) { 
    char extractedChar = [s characterAtIndex:i]; 

    [digits addObject:[NSString stringWithFormat:@"%c", extractedChar]]; 
} 
+0

예, 작동하지만이 작업을 수행하면 같은 문자열에 두 자리 숫자가 표시됩니다. 만약 내가 그것을 2 개의 줄로 나누고 싶다면 어떨까요? 감사합니다 – mat

+0

귀하의 사건을 처리하기 위해 제 대답을 편집했습니다. –

+0

+1, 좋은 답변 .. –

1

시도가 NSLog()를 사용 firstThird의 값을 인쇄, 정확히 잡고 무엇을보고, 당신은 코드, 올바른 것

사용 characterAtIndexNSString 알려진 위치에서 문자를 추출하기위한 기능

- (unichar)characterAtIndex:(NSUInteger)index 

아래와 같이 사용하십시오.

NSString *FirstDigit = [NSString stringWithFormat:@"%c", [myString characterAtIndex:0]]; 
NSString *SecondDigit = [NSString stringWithFormat:@"%c", [myString characterAtIndex:1]]; 
관련 문제