如果我有這樣的方法:當返回值是一個不可變的對象時,返回一個可變對象是不好的做法嗎?
+ (NSDictionary *)dictionaryFromQueryString:(NSString *)queryString
{
NSMutableDictionary *this = [[NSMutableDictionary alloc] init];
NSArray *fields = [queryString componentsSeparatedByString:@"&"];
for(NSString *field in fields)
{
NSArray *fieldParts = [field componentsSeparatedByString:@"="];
NSString *value = @"";
if(fieldParts.count > 1)
{
value = [[fieldParts subarrayWithRange:NSMakeRange(1, fieldParts.count - 1)] componentsJoinedByString:@"="];
}
[this setObject:unescape(value) forKey:unescape(fieldParts[0])];
}
return this;
}
它是那麼不好的做法,我返回NSMutableDictionary
而不是NSDictionary
?
我應該用return [this copy];
將它轉換成NSDictionary
嗎?
我使用ARC反正... – Tyilo