Я нашел код, который используется для экранирования символов html. У меня есть вопрос об этом коде. как вы можете видеть, это «alloc» s, а не «release». это вызывает утечки памяти? это должно быть выпущено?
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