我發現了一個用於轉義html字符的代碼。我對此代碼有疑問。正如你可以看到它「alloc」並且不「釋放」它。它會導致內存泄漏嗎?它必須被釋放?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