我想通过来自不同类的几个方法传输特定容器的数组/ NSArray。在 C++ 中,我会使用结构向量之类的东西。
容器可以用这样的结构表示:
struct test
{
NSString * a;
NSString * b;
NSNumber * c;
}
容器基本上需要保存一些字符串和数字。
在Objective C中使用结构是否有替代方法?是否有其他一些“容器”可以用来发送数据?
我想通过来自不同类的几个方法传输特定容器的数组/ NSArray。在 C++ 中,我会使用结构向量之类的东西。
容器可以用这样的结构表示:
struct test
{
NSString * a;
NSString * b;
NSNumber * c;
}
容器基本上需要保存一些字符串和数字。
在Objective C中使用结构是否有替代方法?是否有其他一些“容器”可以用来发送数据?
我建议创建一个简单的对象,如下所示(在我的浏览器中编译):
@interface Container : NSObject {
NSString* a;
NSString* b;
NSString* c;
}
@property (retain) NSString* a;
@property (retain) NSString* b;
@property (retain) NSString* c;
@end
@implementation Container
@synthesize a, b, c;
- (void) dealloc {
[a release];
[b release];
[c release];
[super release];
}
@end
我认为这正是对象的目的(@interface),事实上,objective-c 类归根结底只是结构,苹果开发人员文档鼓励您创建类而不是使用结构。
NS(Mutable)Dictionary
can act as a generic container for such things, but it's usually better to create a class instead of using dictionaries that always have the same keys.