Copy or save NSString parameter?

I am developing an iOs 4 app with latest SDK and XCode 4.2

I have a question about the NSString parameters. This is my class definition:

#import <Foundation/Foundation.h>

@interface BlogEntry : NSObject
    NSString* title;
    NSString* text;
    NSDate* date;
    NSString* photo;

- (id)initWithTitle:(NSString*)titulo text:(NSString*)texto date:(NSDate*)fecha photo:(NSString*)foto;



And the implementation:

#import "BlogEntry.h"

@implementation BlogEntry

- (id)initWithTitle:(NSString*)titulo text:(NSString*)texto date:(NSDate*)fecha photo:(NSString*)foto
    if (self = [super init])
        title = titulo;
        text = texto;
        date = fecha;
        photo = foto;
    return self;



Can I save the parameters initWithTitle

? Or maybe I have to copy them?


source to share

1 answer

If ARC, no. If not ARC, yes.

For NSString

ivars usually copy

. For NSDate

ivar, retain

. Reason copy

c NSString

is in case it NSMutableString

is passed in your method init

. Copying the parameter prevents your class from changing it. Thus, it provides encapsulation.



All Articles