简体   繁体   English

这两种init方法有什么区别?

[英]What is the difference between these two init methods?

As far as I know, the first one you have to release when you are done, and the second one you don't. 据我所知,第一个必须在完成后释放,而第二个则不需要。 Why would you use the first initialization over the second? 为什么要在第二个初始化中使用第一个初始化?

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"title", @"text", nil];

And

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"title", @"text", nil];

You are correct; 你是对的; with the first version, you will need to explicitly release the object. 对于第一个版本,您将需要显式释放该对象。 The first version is useful if you will be assigning it to an instance variable in your class; 如果将第一个版本分配给类中的实例变量,则第一个版本非常有用。 ie it is a dictionary that will live beyond the current UI event (the latter object is autoreleased and is not available later on in your program). 也就是说,它是一个将在当前UI事件之后继续存在的字典(后一个对象是自动释放的,以后在程序中将不可用)。

For example: 例如:

-(id)init
{
    if (self = [super init])
    {
        myDict = [[NSDictionary alloc] init ...];
    }
    return self;
}

-(void)dealloc
{
    [myDict release];
    [super dealloc];
}

As others have said, in one sense there is no difference between your two examples, just that the first one returns a retained object that will not be dealloc'd until you expicitly release it, and the second one is an autoreleased object that will be automatically released at the beginning of the next run loop unless you explicitly retain it before then. 正如其他人所说,从某种意义上说,您的两个示例之间没有什么区别,只是第一个示例返回一个保留对象,直到您明确释放它为止,该对象不会被取消分配,而第二个示例是一个将被释放的自动释放对象。除非在此之前明确保留它,否则它将在下一个运行循环开始时自动释放。

However the key part to your question is: 但是,您的问题的关键部分是:

Why would you use the first initialization over the second? 为什么要在第二个初始化中使用第一个初始化?

The commonly regarded approach (support by Apple documentation) is that using autoreleased objects is absolutely fine in most cases. 普遍认为的方法(Apple文档的支持)是,在大多数情况下使用自动释放的对象绝对可以 I would not agree with Peter Lewis here about the iPhone being so "underpowered" that you need to avoid autoreleased objects. 在这里,我不会同意彼得·刘易斯(Peter Lewis)所说的iPhone的“功能不足”,以至于需要避免自动释放的物体。 The additional overhead on the phone on supplying autoreleased objects in most apps is miniscule. 在大多数应用程序中提供自动释放对象的电话的额外开销很小。 Therefore feel free to use autoreleased objects generally in your code, and just explicitly retain them if you need to hang on to them. 因此,可以在代码中随意使用自动释放的对象,如果需要使用它们,只需显式地保留它们即可。

However, where it does make a difference is in tight loops . 但是, 确实有帮助的地方是紧密的循环 If you are running a while loop for example which executes 1000 times, and you are creating objects within that loop that you will not need afterwards, then you should explicitly create new non-autoreleased objects and explicitly release them. 例如,如果您正在运行一个执行1000次的while循环,并且正在该循环中创建以后将不需要的对象,则应显式创建新的非自动释放的对象并显式释放它们。 This is because the autorelease pool will not (by default) empty inside the tight loop. 这是因为自动释放池在紧密循环内不会(默认)为空。 Creating a large bunch of autoreleased objects could well start to hit the limitations of the device's memory. 创建大量自动释放的对象很可能会开始遇到设备内存的限制。

So in summary; 总而言之, use 2) most of the time, but use 1) if you're going to be creating and ditching a lot of objects at one time. 大多数情况下使用2),但如果要一次创建和抛弃许多对象,则使用1)。

Hope that helps 希望能有所帮助

The first init method is an instance method (works on the instance returned by [NSMutableDictionary alloc] ) and is not an autoreleased object, with a retain count of 1. 第一个init方法是一个instance方法(可在[NSMutableDictionary alloc]返回的实例上工作),并且不是自动释放的对象,保留计数为1。

The second is a class method, and returns an autoreleased object (with essentially no retain count). 第二个是class方法,并返回一个自动释放的对象(基本上没有保留计数)。

NSMutableDictionary类自动释放第二个,其中第一个处理您自己的内存管理。

Read the memory management rules . 阅读内存管理规则 9 short paragraphs, you can read in about a minute. 9个简短的段落,您可以在一分钟内阅读。 They clearly explain the difference between the two. 他们清楚地解释了两者之间的区别。

As to why you should use one or the other, it depends on the platform. 至于为什么要使用其中一个,则取决于平台。

On the iPhone, which is under powered and low on memory, you would generally prefer the former unless you are going to be forced to use autorelease (for example, if you are going to return it from a method whose name does not begin with “alloc” or “new” or contains “copy”). 在电量不足且内存不足的iPhone上,通常会选择前者,除非您将被迫使用自动释放功能(例如,如果要从名称不以“ alloc”或“ new”或包含“ copy”)。 If you are going to autorelease it, then use the second form. 如果要自动释放它,请使用第二种形式。 Otherwise, use the first form and release it explicitly yourself in the method. 否则,请使用第一种形式并在方法中自己明确释放它。

On the Mac, which has more than enough power and memory to make the autorelease pool a trivial detail (in all but the most pathelogical cases), you can use the second form for any case. 在Mac上,它具有足够的功能和内存,可以使自动释放池成为琐碎的细节(在大多数病理情况下除外),在任何情况下都可以使用第二种形式。 Normally if you are going to keep it around you would assign it to a copy/retain property so you would not need to retain it. 通常,如果要保留它,可以将其分配给copy / retain属性,这样就无需保留它。 The exception is in an init method, where you can't use setters, or in setters you are implementing manually, and thus need to assign to the ivar yourself. 例外是init方法中,在该方法中您不能使用设置器,或者在手动实现的设置器中,因此需要自己分配给ivar。 In those cases you would often use the former. 在这种情况下,您通常会使用前者。

But all of that is just style/premature optimization (not necessarily bad in this case, since in is a low level noise kind of optimization that profiling will not detect and that will just generally slow you app down by a little). 但是,所有这些只是样式/过早的优化(在这种情况下不一定很糟糕,因为这是一种低噪声类型的优化,性能分析不会检测到这种情况,并且通常只会使您的应用运行速度降低一点)。 The important thing is to read the rules and follow what they say, and if you do that, you can use either form as you please. 重要的是要阅读规则并遵守规则,如果您这样做,则可以随意使用其中任何一种形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这两种NSString方法之间的区别 - Difference between these two NSString methods 方法“插入子视图”和“ presentModalViewController”之间有什么区别? - What is the difference between methods “insert subview” and “presentModalViewController”? 行动方法和协议方法之间有什么区别? - What’s the difference between action methods and protocol methods? 在 ViewController 之间切换时两种方法的区别 - Difference Between Two Methods When Switching Between ViewControllers 可可接触。 类方法初始化版本alloc / init之间的语义区别是什么? - Cocoa-Touch. What is the semantic difference between class method init versions alloc/init? 可可接触。 这些NSMutableData方法之间的确切区别是什么? - Cocoa-Touch. What Exactly is the Difference Between These NSMutableData Methods? 这两种在Objective-C中分配内存的方式有什么区别? - What is the difference between these two ways of allocating memory in Objective-C? 这两个初始化片段之间有什么区别? 哪一个是正确的? - What'S the difference between these two initialization snippets? Which one is correct? [NSMutableData数据]和[[NSMutableData alloc] init]之间的区别 - Difference between [NSMutableData data] and [[NSMutableData alloc]init] [[UIImageView new] init]和[[UIImageView alloc] init]之间的区别 - Difference between [[UIImageView new] init] and [[UIImageView alloc] init]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM