任何人都可以查看以下方法并告诉我是否有必要在使用后释放字典键的枚举器?我是 Objective c 的新手,并不总是确定何时释放从 SDK 方法返回的变量。我想 keyEnumerator 方法返回一个自动释放的实例,所以当我完成它时我不必关心它。对?
嗯..也许这种方法还有什么问题?;)
#import "NSDictionaryURLEncoding.h"
#import <Foundation/NSURL.h>
@implementation NSDictionary(URLEncoding)
-(NSString *)urlEncoded
{
NSEnumerator *keyEnum = [self keyEnumerator];
NSString *currKey;
NSString *currObject;
NSMutableString *result = [NSMutableString stringWithCapacity: 64];
while ((currKey = [keyEnum nextObject]) != nil)
{
if ([result length] > 0)
{
[result appendString: @"&"];
}
currObject = [[self objectForKey: currKey] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
currKey = [currKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[result appendString: [NSString stringWithFormat:@"%@=%@", currKey, currObject]];
}
return result;
}
@end