2013-01-05 2 views
0

객관적인 C 방법에 대한 가장 기본적인 문제가 있습니다. 에서 제안을 바탕으로 : 나는 잘린있는 NSString을 반환하는 방법을 쓰기 위해 노력하고 있어요 How can I truncate an NSString to a set length?iOS 목표 C NSString.length가 예상 값을 반환하지 않음

. 그러나 작동하지 않습니다. 예를 들어 "555"로 보낼 때 길이 (변수 'test'로 표시됨)는 0으로 되돌아갑니다. int int 테스트 후 중단 점을 설정하고 변수 fullString 및 test를 가리키면이 점을 확인할 수 있습니다. 어떻게 든 fullString 또는 다른 것에 포인터를 역 참조 할 필요가 있습니까? 나는 객관적인 C.의 완전한 초보자 다. 많은 감사

-(NSString*) getTruncatedString:(NSString *) fullString { 

    int test = fullString.length; 
    int test2 = MIN(0,test); 
    NSRange stringRangeTest = {0, MIN([@"Test" length], 20)}; 

    // define the range you're interested in 
    NSRange stringRange = {0, MIN([fullString length], 20)}; 

    // adjust the range to include dependent chars 
    stringRange = [fullString rangeOfComposedCharacterSequencesForRange:stringRange]; 

    // Now you can create the short string 
    NSString* shortString = [_sentToBrainDisplay.text substringWithRange:stringRange]; 

    return shortString; 

} 

나는 의견과 연구에 의거하여 그것을 얻었다. 모두 감사합니다. 경우 사람이 관심 :

-(NSString*) getTruncatedString:(NSString *) fullString { 

if (fullString == nil || fullString.length == 0) { 
    return fullString; 
} 
NSLog(@"String length: %d", fullString.length); 

// define the range you're interested in 
NSRange stringRange = {MAX(0, (int)[fullString length]-20),MIN(20, [fullString length])}; 


// adjust the range to include dependent chars 
stringRange = [fullString rangeOfComposedCharacterSequencesForRange:stringRange]; 

// Now you can create the short string 
NSString* shortString = [fullString substringWithRange:stringRange]; 

return shortString; 

}

+2

"어떻게 든 포인터를 fullString으로 참조 해제해야합니까?"- 물론 아닙니다. Objective-C에서 객체에 대한 포인터를 절대 참조 해제하지 마십시오. –

+1

풀 스트링이 0이 아닌 것은 확실합니까? –

+2

_sentToBrainDisplay.text 값이란 무엇입니까? –

답변

1

확인이 방법이 아닌 "555"@"555"을 전달하는 경우. 더 좋은 방법은

NSLog(@"String length: %d", fullString.length)입니다.

+0

Nockolay에게 NSLog에 대한 의견을 보내 주셔서 감사합니다. _sentToBrainDisplay에 대한 Ramy의 의견은 저를 문제로 안내했습니다. 나중에 이것을보고있는 사람들을 위해 원래의 질문에 해결책을 게시했습니다. – Dave

관련 문제