English 中文(简体)
在目标C中分配记忆的这两种方法有何区别?
原标题:What is the difference between these two ways of allocating memory in Objective-C?

我感到困惑的是,在目标C中分配记忆的适当手段是,我有一位有叙文的人。 我可以采取两种方法:

   NSMutableDictionary *alpha = [[NSMutableDictionary alloc] init];

   NSMutableDictionary *alpha = [NSMutableDictionary dictionary];

What is the difference between them? I know the first allocates mem或y f或 alpha, but what about the second?

Which of these is recommended as the best practice f或 allocating mem或y?

最佳回答
[NSMutableDictionary dictionary];

确实与以下方面相同:

[[[NSMutableDictionary alloc] init] autorelease];

这只是拯救了你们一些打字。 只要你知道保留物体与自动释放物体之间的区别,你就会使用。 如果你重新使用非洲排雷中心,那么你甚至不需要知道这一点。

The convention is:

  1. If you see init, new or copy: it s a retained object.
  2. If the method name starts with the class name (sans the framework prefix), it s an autoreleased object.
问题回答

这两行都将为甲型六氯环己烷分配记忆,差异是甲型六氯环己烷是自动释放物体。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签