可可触感。这些 NSMutableData 方法之间到底有什么区别?
我有点不清楚的一件事是这些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该类方法创建并自动释放 NSMutableArray 对象。
实例方法初始化一个您必须自己分配的对象。您编写的代码实际上不会执行任何操作,因为
myMutableArrayInstance
为nil
。类方法大致相当于:正如 Peter Hosey 在注释中指出的那样,它的真正含义是:
如果
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
isnil
. The class method is roughly equivalent to this:And as Peter Hosey notes in comments, it really means this:
which will have different results from the above if the
initWithCapacity:
method returns a different object.