复制或保留NSString参数? [英] Copy or retain NSString parameter?
问题描述
我正在使用最新的SDK和XCode 4.2开发iOs 4应用程序
I'm developing an iOs 4 app with latest SDK and XCode 4.2
我有一个关于NSString参数的问题。这是我的类定义:
I have a question about 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;
@end
并实施:
#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;
}
@end
code> initWithTitle 参数?
May I need to retain initWithTitle
parameters? Or, may I have to copy them?
推荐答案
如果ARC,没有。如果不是ARC,是的。
If ARC, no. If non-ARC, yes.
对于 NSString
ivars,通常 code>。对于
NSDate
ivar, retain
。使用 NSString
的 copy
的原因是在 NSMutableString
在 init
方法中传递。复制参数可防止它被您的类所改变。因此,它确保封装。
For the NSString
ivars, usually copy
. For the NSDate
ivar, retain
. The reason for copy
with NSString
is in case an NSMutableString
is passed in your init
method. Copying the parameter prevents it from being mutated by your class. Thus, it ensures encapsulation.
这篇关于复制或保留NSString参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!