创建只包含给定类的对象的数组 [英] Creating an array that contains only objects of a given class
问题描述
好的,所以我有下面的代码(Objective-C FYI),我想知道如果我想创建一个NSMutableArray c_data
对象,我将如何去这样做?它有点类似于声明一个 List< c_data>
@interface c_data:NSObject {
double值;
int label;
int ID;
}
@property double value;
@property int label;
@property int ID;
- (c_data *)init;
- (c_data *)initWithValue:(double)value;
@end
@implementation c_data
@synthesize值,label,ID;
- (c_data *)init {
return self;
}
- (c_data *)initWithValue:(double)val {
value = val;
return self;
}
@end
如果你看看 feat_data
,我试图使 cData
数组 c_data
。我已经包括我的尝试,但我不认为这是正确的,因为 c_data
不是一个数组。有什么建议么?
@interface feat_data:NSObject {
NSMutableArray * nData;
NSMutableArray * cData;
char类型;
}
@property(nonatomic,retain)NSMutableArray * nData;
@property(nonatomic,retain)NSMutableArray * cData;
@property char type;
- (feat_data *)init;
@end
@implementation feat_data
@synthesize nData,cData,type;
- (feat_data *)init {
nData = [[NSMutableArray alloc] init];
c_data * c_dataInstance = [[c_data alloc] init];
cData = [[NSMutableArray alloc] initWithArray:c_dataInstance];
return self;
}
@end
$ p> [NSMutableArray addObject:[[[c_data alloc] init] autorelease]];
未键入Objective-C数组。看来你有一些C ++不学习。
在一个相关的注释,你的inits是相当糟糕。您还需要调用super init,如下:
- (id)init {
self = [super在里面];
if(self!= nil){
//在这里初始化。
}
return self;
}
Ok, so I have the code below (Objective-C FYI) and I was wondering if I want to create an NSMutableArray of c_data
objects, how would I go about doing that? It's sort of like declaring a List<c_data> cData
in C#.
@interface c_data : NSObject {
double value;
int label;
int ID;
}
@property double value;
@property int label;
@property int ID;
-(c_data*) init;
-(c_data*) initWithValue:(double)value;
@end
@implementation c_data
@synthesize value, label, ID;
-(c_data*) init {
return self;
}
-(c_data*) initWithValue:(double)val {
value = val;
return self;
}
@end
If you look at the class feat_data
, I'm trying to make cData
an array of the class c_data
. I have included my attempts at it, but I don't think it's right because c_data
isn't an array. Any suggestions?
@interface feat_data : NSObject {
NSMutableArray *nData;
NSMutableArray *cData;
char type;
}
@property(nonatomic, retain) NSMutableArray *nData;
@property(nonatomic, retain) NSMutableArray *cData;
@property char type;
-(feat_data*)init;
@end
@implementation feat_data
@synthesize nData, cData, type;
-(feat_data*)init {
nData = [[NSMutableArray alloc] init];
c_data *c_dataInstance = [[c_data alloc] init];
cData = [[NSMutableArray alloc] initWithArray:c_dataInstance];
return self;
}
@end
[NSMutableArray addObject:[[[c_data alloc] init] autorelease]];
Objective-C arrays aren't typed. It seems you have some C++ unlearning to do.
On a related note, your inits are pretty bad. You need to call super init as well, as such:
- (id)init {
self = [super init];
if (self != nil) {
//Initialize here.
}
return self;
}
这篇关于创建只包含给定类的对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!