Does copy allocate the required memory?
Here is an example taken from Apple iPhone SDK project:
@interface EADSessionController : NSObject <EAAccessoryDelegate, NSStreamDelegate> {
EAAccessory *_accessory;
EASession *_session;
NSString *_protocolString;
NSMutableData *_writeData;
NSMutableData *_readData;
}
...
// initialize the accessory with the protocolString
- (void)setupControllerForAccessory:(EAAccessory *)accessory withProtocolString:(NSString *)protocolString
{
[_accessory release];
_accessory = [accessory retain];
[_protocolString release];
_protocolString = [protocolString copy];
}
My understanding is that "copy" will also allocate the memory required to copy the protocolString object passed as an 开发者_如何转开发argument, and therefore there is not need to allocate (alloc) something before copying.
Am I right ?
Regards, Apple92
Correct. The copy starts out with a retain count of (at least) one, and you are responsible for eventually releasing it. (See Object Ownership Policy.)
精彩评论