简体   繁体   English

可可接触。 这些NSMutableData方法之间的确切区别是什么?

[英]Cocoa-Touch. What Exactly is the Difference Between These NSMutableData Methods?

One thing I'm a bit unclear on is the difference between these NSMutableArray Methods: 我不清楚的一件事是这些NSMutableArray方法之间的区别:

// Class Method Style

NSMutableData *myMutableDataInstance = [NSMutableData dataWithLength:WholeLottaData];

and

// Instance Method Style

NSMutableData *myMutableDataInstance = nil;

myMutableDataInstance = [[[NSMutableData alloc] initWithLength:WholeLottaData]] autorelease];

Under the hood, what eactly is the class method doing here? 在后台,类方法实际上在做什么? How does it differ from the instance method? 它与实例方法有何不同?

Cheers, Doug 干杯,道格

The class method creates and autoreleases an NSMutableArray object. 类方法创建并自动释放NSMutableArray对象。

The instance method initialzes an object that you have to allocate yourself. 实例方法将初始化您必须自行分配的对象。 The code you've written won't actually do anything, because myMutableArrayInstance is nil . 您编写的代码实际上不会执行任何操作,因为myMutableArrayInstancenil The class method is roughly equivalent to this: 类方法大致等效于此:

NSMutableArray *myMutableArrayInstance = [NSMutableArray alloc];
[myMutableArrayInstance initWithCapacity:WholeLottaData];
[myMutableArrayInstance autorelease];

And as Peter Hosey notes in comments, it really means this: 正如彼得·霍西(Peter Hosey)在评论中指出的那样,这实际上意味着:

NSMutableArray *myMutableArrayInstance = [[[NSMutableArray alloc]
                                           initWithCapacity:WholeLottaData]
                                           autorelease];

which will have different results from the above if the initWithCapacity: method returns a different object. 如果initWithCapacity:方法返回不同的对象,则结果将与上面的结果不同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM