要创建一个空字典,我应该拨打[NSMutableDictionary dictionary]
还是[[NSMutableDictionary alloc] init]
?要创建一个空字典,[NSMutableDictionary字典]或[[NSMutableDictionary alloc] init]?
有什么区别?
要创建一个空字典,我应该拨打[NSMutableDictionary dictionary]
还是[[NSMutableDictionary alloc] init]
?要创建一个空字典,[NSMutableDictionary字典]或[[NSMutableDictionary alloc] init]?
有什么区别?
[NSMutableDictionary dictionary]
相同[[[NSMutableDictionary alloc] init] autorelease]
不同的是,如果你使用NSMutableDictionary* myDictionary = [[NSMutableDictionary alloc] init]
你将不得不再打[myDictionary release]
当你用它做。否则你会泄漏内存。如果使用其他方法,则不必担心内存管理。
您仍然需要担心使用自动发布实例的内存管理。考虑例如在紧密的循环中创建许多数组而不会消耗相应的自动释放池。 – 2010-11-11 09:08:25
为了完整起见,一些年后,我们还有其他更短的方式来ALLOC词典:
NSMutableDictionary* dictionary = @{}.mutableCopy;
感谢。对于iPhone编程,我认为[[NSMutableDictionary alloc] init]是首选。 – 2010-11-11 08:54:45
@Stephen:???你为什么这么想?当然,在许多情况下,在完成它们之后,你可能会更好地释放事物,但是也可以在紧密循环中的每n个周期之后关闭一个自动释放池以管理内存使用情况。然而,正如你所说的那样,你的评论过于宽泛而不真实。 – 2010-11-11 15:39:22