可可触感。这些 NSMutableData 方法之间到底有什么区别?

发布于 2024-08-09 06:32:13 字数 429 浏览 5 评论 0原文

我有点不清楚的一件事是这些 NSMutableArray 方法之间的区别:

// Class Method Style

NSMutableData *myMutableDataInstance = [NSMutableData dataWithLength:WholeLottaData];

// Instance Method Style

NSMutableData *myMutableDataInstance = nil;

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

幕后,类方法到底在做什么?它与实例方法有什么不同?

干杯, 道格

One thing I'm a bit unclear on is the difference between these NSMutableArray Methods:

// 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-08-16 06:32:13

该类方法创建并自动释放 NSMutableArray 对象。

实例方法初始化一个您必须自己分配的对象。您编写的代码实际上不会执行任何操作,因为 myMutableArrayInstancenil。类方法大致相当于:

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

正如 Peter Hosey 在注释中指出的那样,它的真正含义是:

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

如果 initWithCapacity: 方法返回不同的对象,则其结果将与上面的不同。

The class method creates and autoreleases an NSMutableArray object.

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. 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:

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

which will have different results from the above if the initWithCapacity: method returns a different object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文