创建仅包含给定类的对象的数组
好的,我有下面的代码(Objective-C FYI),我想知道如果我想创建一个 c_data
对象的 NSMutableArray,我该怎么做呢? 这有点像声明一个 List
。
@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
如果您查看类 feat_data
,我会尝试将 cData
设为类 c_data
的数组。 我已经包含了我的尝试,但我认为这是不对的,因为 c_data
不是数组。 有什么建议么?
@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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Objective-C 中不存在静态类型/模板/泛型集合之类的东西。 基本上,强类型集合的要点是在编译时提供静态类型安全。 对于像 Objective-C 这样动态的语言来说,这种方法没有什么意义。 解决 Objective-C 集合中不同对象类型问题的方法是仅插入适当的对象类型。 (另外,请记住,数组将保留它包含的对象,因此,如果您插入一个新对象而不释放它,并且丢失了指向它的指针,那么您就会泄漏内存。)
如果您考虑一下,泛型的最大好处之一能够将集合中的对象直接检索到静态类型变量中,而无需进行强制转换。 在 Objective-C 中,您可以只存储到 id 变量并发送您喜欢的任何消息,而不必担心 ClassCastException,或者编译器抱怨对象没有(可能没有?)实现该方法您正在尝试调用。 如果需要,您仍然可以静态键入变量并转换结果,但更简单的方法是使用动态类型(以及
-isKindOfClass:
和-respondsToSelector:
(如果需要)。顺便说一句,这个问题在 Stack Overflow 上有几个相关的版本。 了解要搜索的术语(“通用”、“强类型”或“模板”)可以帮助找到它们。 以下是一些:
最后,我同意 William 的观点 - 在您提供的示例中,您的 init 方法非常令人震惊。 您最好学习并遵守 Apple 的规则 在 Objective-C 中分配和初始化对象。 它需要打破其他语言的习惯,但它会在未来的某个时刻让你免去无尽的疯狂时间。 :-)
There is no such thing as statically typed / template / generic collections in Objective-C. Basically, the point of a strongly-typed collection is to provide static type safety at compile time. Such an approach makes little sense in a language as dynamic as Objective-C. The approach to the problem of disparate object types in Objective-C collections is to only insert the appropriate object type(s). (Also, remember that the array will retain objects it contains, so if you insert a new object without releasing and you lose the pointer to it, you're leaking memory.)
If you think about it, one of the biggest benefits to generics is being able to retrieve objects from the collection directly into a statically-typed variable without casting. In Objective-C, you can just store to an
id
variable and send whatever message you like without fretting about a ClassCastException, or the compiler complaining that an object doesn't (may not?) implement the method you're attempting to invoke. You can still statically type variables and cast results if desired, but the easier approach is to use dynamic typing (and-isKindOfClass:
and-respondsToSelector:
if necessary).Incidentally, there are several related incarnations of this question on Stack Overflow. Knowing the term(s) to search for ("generic", "strongly-typed", or "template") can help find them. Here are a few:
Finally, I agree with William — your init methods are pretty egregious in the sample you provided. You'd do well to learn and heed Apple's rules of Allocating and Initializing Objects in Objective-C. It requires breaking habits from other languages, but it will save you endless hours of insanity at some point down the road. :-)
Objective-C 数组没有类型。 看来你还需要学习一些 C++ 知识。
与此相关的是,你的初始化非常糟糕。 您还需要调用 super init,如下所示:
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:
您将创建一个 NSMutableArray 并向其中插入
c_data
对象。You would create an NSMutableArray and insert
c_data
objects into it.