问题
我正在尝试在 Objective-C 中创建一个对象。我知道该怎么做,但我对实现文件中的方法有一些疑问。
对象:
@interface Program : NSObject {
NSString *sid;
NSString *le;
NSString *sd;
NSString *pid;
NSString *n;
NSString *d;
NSString *url;
}
@property (nonatomic, retain) NSString *sid;
@property (nonatomic, retain) NSString *le;
@property (nonatomic, retain) NSString *sd;
@property (nonatomic, retain) NSString *pid;
@property (nonatomic, retain) NSString *n;
@property (nonatomic, retain) NSString *d;
@property (nonatomic, retain) NSString *url;
@end
问题
我应该只实现 dealloc
和 init
..我对吗?
此外,我没有特殊的初始化,所以我应该保留默认的 init
方法吗?
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
最佳答案
您需要合成属性,如果您不需要任何自定义初始化,那么您可以保持 init
方法不变。实际上这里不需要实现init
。但是在 dealloc
中你需要释放字符串。
@implementation Program
@synthesize sid;
// ... synthesize others
- (void)dealloc {
[sid release];
// ... release others
[super dealloc];
}
@end
关于iphone - 创建对象 - Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6959020/