html 문자를 이스케이프하는 데 사용되는 코드를 발견했습니다. 이 코드에 대한 질문이 있습니다. 당신이 볼 수 있듯이 "할당"하고 그것을 "공개"하지 않습니다. 메모리 누수가 발생합니까? 반드시 공개해야합니까?objective c 메모리 관리
htmlEscapes = [[NSDictionary alloc] initWithObjectsAndKeys: // @"&", @"&", @"<", @"", @"'", @"'", @""", @"\"", nil ];
덕분에 ...
학급 전체
#import "NSString+HTML.h" @implementation NSString (HTMLExtensions) static NSDictionary *htmlEscapes = nil; static NSDictionary *htmlUnescapes = nil; + (NSDictionary *)htmlEscapes { if (!htmlEscapes) { htmlEscapes = [[NSDictionary alloc] initWithObjectsAndKeys: // @"&", @"&", @"<", @"", @"'", @"'", @""", @"\"", nil ]; } return htmlEscapes; } + (NSDictionary *)htmlUnescapes { if (!htmlUnescapes) { htmlUnescapes = [[NSDictionary alloc] initWithObjectsAndKeys: // @"&", @"&", @"", @">", @"'", @"'", @"\"", @""", nil ]; } return htmlEscapes; } static NSString *replaceAll(NSString *s, NSDictionary *replacements) { for (NSString *key in replacements) { NSString *replacement = [replacements objectForKey:key]; s = [s stringByReplacingOccurrencesOfString:key withString:replacement]; } return s; } - (NSString *)htmlEscapedString { return replaceAll(self, [[self class] htmlEscapes]); } - (NSString *)htmlUnescapedString { return replaceAll(self, [[self class] htmlUnescapes]); } @end