J'ai trouvé un code qui est utilisé pour échapper les caractères html. J'ai une question à propos de ce code. comme vous pouvez le voir "alloc" et ne "libère" pas. cela provoque-t-il des fuites de mémoire? doit-il être libéré?objectif c gestion de la mémoire
htmlEscapes = [[NSDictionary alloc] initWithObjectsAndKeys: // @"&", @"&", @"<", @"", @"'", @"'", @""", @"\"", nil ];
grâce ...
classe entière
#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