아마도 NSScanner
을 사용하여이 방법을 직접 작성하는 것이 가장 쉽습니다. 엔티티 구문은 단순한 대체 목록보다 복잡합니다. 즉, 당신은 지원해야합니다
- & # D; 여기서 D은 십진수입니다.
- & # x * H *; H는 16 진수입니다
을 (대문자와 소문자 모두 OK입니다) 그리고 당신은 (a list in the HTML 4 specification 거기) 명명 된 개체에 대한 매핑 테이블이 필요합니다. 매핑의 사전을 초기화 할 수 loadEntityMappingTable()
를 구현하고 그것을 작동합니다, 어딘가
static NSDictionary *entityDict;
if (!entityDict)
entityDict = loadEntityMappingTable();
NSScanner *scanner = [NSScanner scannerWithString:myHTMLString];
NSMutableString *result = [NSMutableString string];
[scanner setCharactersToBeSkipped:nil]; // Don’t skip whitespace
while (![scanner isAtEnd]) {
NSString *chunk, *name;
if ([scanner scanUpToString:@"&" intoString:chunk])
[result appendString:chunk];
if ([scanner scanString:@"#" intoString:NULL]) {
unsigned uch;
NSUInteger scanLoc;
BOOL hex = NO;
// This is a numeric reference
if ([scanner scanString:@"x" intoString:NULL]) {
hex = YES;
scanLoc = [scanner scanLocation];
if (![scanner scanHexInt:&uch]) {
// If we fail, show the entire thing in the result string
[result appendString:@"&#x"];
continue;
}
} else {
int ich;
scanLoc = [scanner scanLocation];
if (![scanner scanInt:&ich]) {
// If we fail, show the entire thing
[result appendString:@"&#"];
continue;
}
if (ich < 0) {
// Bad Unicode code point
[result appendString:@"&#"];
[scanner setScanLocation:scanLoc];
continue;
}
uch = (unsigned)ich;
}
// You may also care to prohibit control codes (depending on your application)
// i.e. uch < 0x20 || uch >= 0x7f && uch < 0xa0
if (uch >= 0xd800 && uch <= 0xdfff || uch > 0x10ffff) {
// Bad Unicode code point; show it in the result
[result appendString:hex ? @"&#x" : @"&#"];
[scanner setScanLocation:scanLoc];
continue;
}
if (![scanner scanString:@";" intoString:NULL]) {
// Unterminated; show it in the result
[result appendString:hex ? @"&#x" : @"&#"];
[scanner setScanLocation:scanLoc];
continue;
}
if (uch < 0xffff)
[result appendFormat:@"%C", uch];
else {
unichar lo, hi;
hi = 0xd800 | (uch >> 10);
lo = 0xdc00 | (uch & 0x3ff);
[result appendFormat:@"%C%C", hi, lo];
}
continue;
}
if ([scanner scanUpToString:@";" intoString:&name]) {
NSString *ch;
if (![scanner scanString:@";" intoString:NULL]) {
// Unterminated; show it in the result
[result appendFormat:@"&%@", name];
continue;
}
ch = [entityDict objectForKey:[name lowercaseString]];
if (!ch) {
// Unrecognised; show it in the result
[result appendFormat:@"&%@;", name];
continue;
}
[result appendString:ch];
}
}
스틱 그 함수 나 방법 :
다음은 시작하는 (스택 오버플로 작성, 테스트되지 않은) 일부 코드입니다.
FWIW 루프와 NSScanner
을 사용하는이 일반적인 접근 방식은 스크립팅 언어에서 정규 표현식 일치를 사용하여 처리 할 수있는 비슷한 많은 문제에 쉽게 적용 할 수 있습니다.
도대체 "시뮬레이터에서 응용 프로그램을 빌드 할 수 없게 만든다"는 의미는 –
이전 프로젝트에서 GTM을 사용했을 때 시뮬레이터에서 빌드 할 수 없었지만 장치에서 작동했습니다. – johnyu
질문은 분명하지 않습니다. 당신이 만나는 문제가 무엇인지 분명히해야합니다. –