코코아 : NSString은 모든 캐릭터를 제거하지 않습니다
문제
나는 int가 있고 어떤 이유로 든 그것은 16 정도 후에 작동하지 않습니다. 내 코드는 다음과 같습니다.
NSArray *sortedArray;
sortedArray = [doesntContainAnother sortedArrayUsingFunction:firstNumSort context:NULL];
int count2 = [sortedArray count];
//NSLog(@"%d", count2);
int z = 0;
while (z < count2) {
NSString *myString = [sortedArray objectAtIndex:z];
NSString *intstring = [NSString stringWithFormat:@"%d", z];
NSString *stringWithoutSpaces;
stringWithoutSpaces = [[myString stringByReplacingOccurrencesOfString:intstring
withString:@""] mutableCopy];
[hopefulfinal addObject:stringWithoutSpaces];
NSLog(@"%@", [hopefulfinal objectAtIndex:z]);
z++;
}
편집 : 그것은 int가 아니고, stringspaces 라인입니다 ... 나는 그것을 원인하는 원인을 알 수 없습니다.
따라서 (NSLOG, 위의 Z ++ 참조) : 다음과 같습니다.
"여기"
"무엇이든"
"17 뭐든지"
"18 이거"
등.
해결책
나는 이것이 당신의 이전 질문과 관련이 있다고 생각합니다 배열에 포함 된 int별로 nsarray를 정렬, 그리고 당신은 그 질문에서 가지고있는 배열처럼 보이는 배열에서 선행 숫자와 공백을 제거하려고합니다.
"0 Here is an object"
"1 What the heck, here's another!"
"2 Let's put 2 here too!"
"3 Let's put this one right here"
"4 Here's another object"
전체 입력을 모르면 선행 숫자와 값이 z
동기화에서 벗어납니다. 당신은 실제로 선행 숫자가 무엇인지 신경 쓰지 않고 단지 그것을 바모 로스하고 싶어하기 때문에, 나는 숫자를 스캔하고 해당 자릿수가 끝나는 위치에서 하위 문자열을 추출하는 다른 접근법을 추천합니다.
NSArray *array = [NSArray arrayWithObjects:@"1 One",
@"2 Two",
@"5 Five",
@"17 Seventeen",
nil];
NSMutableArray *results = [NSMutableArray array];
NSScanner *scanner;
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
for (NSString *item in array) {
scanner = [NSScanner scannerWithString:item];
[scanner scanInteger:NULL]; // throwing away the BOOL return value...
// if string does not start with a number,
// the scanLocation will be 0, which is good.
[results addObject:[[item substringFromIndex:[scanner scanLocation]]
stringByTrimmingCharactersInSet:whitespace]];
}
NSLog(@"Resulting array is: %@", results);
// Resulting array is: (
// One,
// Two,
// Five,
// Seventeen
// )
)
제휴하지 않습니다 StackOverflow