views:

24

answers:

1

Hi,

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

A: 

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

zem