substringWithRange : NSMakeRange를 사용하여 NSString의 하위 문자열을 가져 오려고합니다. 저장된 사전에서 초기 문자열을 가져오고 있습니다. 저장된 문자열은 agent_AGENTNAME으로 기록되며 agent_ 부분을 제거하려고합니다. 아래 코드는 경우 (이 원유 경우를 비판 주시기) 잘 작동 나는 하드 코드 NSMakeRange에있는 번호 - 그래서NSMakeRange 응용 프로그램이 충돌합니다
NSString* savedAgentName = [NSString stringWithFormat:@"%@", [thisfile substringWithRange:NSMakeRange(6,19)]];
좋아하지만 모든 사람이 분명히 서로 다른 길이의 이름이 때문에 내가해야 이보다 더 역동적입니다. 코드를 다음과 같이 바꿀 때 :
NSString* savedAgentName = [NSString stringWithFormat:@"%@", [thisfile substringWithRange:NSMakeRange(6,[thisfile length])]];
내 응용 프로그램이 다운됩니다. 왜?
//get saved agents
savedAgents = [[NSMutableArray alloc] initWithObjects:@"Select An Agent", nil];
for(int f=0; f<[rootcontents count]; f++) {
NSString* thisfile = [NSString stringWithFormat:@"%@", [rootcontents objectAtIndex:f]];
if ([thisfile rangeOfString:@"agent_"].location != NSNotFound) {
int thisfilelength = [thisfile length];
NSString* savedAgentName = [NSString stringWithFormat:@"%@", [thisfile substringWithRange:NSMakeRange(6,thisfilelength)]];
//NSLog(@"%@", savedAgentName);
[savedAgents addObject:savedAgentName];
}
}
감사 :
여기에 코드의 큰 덩어리입니다.
이 답변에 대해 고맙지 만 도움이 많이되었습니다. 물고기를 가르쳐 +1! – PruitIgoe