<代码>[NSMutableDictionary dictionary]是NSMutableDictionary<>_code>的类别方法。 该表载有对<代码>alloc
和init
的电话,就像你手工操作一样。 由于这是一个非常常用的类别,作者包括这一所谓的“要素方法”,以使他/她的阶层的使用者的生活更加容易。
见。 关于这一模式的更多细节。
As for which is good practice, IMHO you should use the factory method. It makes your own code more readable and saves you typing. Moreover - although I am not sure if this is the case with NSMutableDictionary
- using the factory method makes maintenance easier for both its developers and yourself, because they are e. g. free to change the concrete subclass implementation you will receive from the factory, without having you change your application code.
As correctly pointed out it the comments, factory methods like this return autoreleased instances of objects, so if you only use them locally in a method, there is no need to worry about them from a memory management point of view. For more details on memory management, autorelease and the new ARC runtime with automatic reference counting, you should deep dive into Apple s documentation on the topic, if you haven t already.