在 NSArray 中存储 2 个 int 和一个 char 的元组,
我想在 NSArray 中存储 2 个整数和一个字符的元组。 有没有比声明一个包含 2 个 int 和 char 的类更简单的方法?
我尝试了这种方法并且有效,但对我来说似乎相当复杂。有没有更好、更简单的方法?
@interface Container : NSObject
@property NSInteger a;
@property NSInteger b;
@property char c;
@end
@implementation Container
@synthesize a = _a;
@synthesize b = _b;
@synthesize c = _c;
-(Container*) initWitha:(NSInteger) a andB:(NSInteger) b andC: (char) c
{
if ((self = [super init])) {
self.a = a;
self.b = b;
self.c = c;
}
return self;
}
@end
...
//usage
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: [[Container alloc] initWitha:5 andB:6 andC:'D']];
谢谢
I want to store tuples of 2 ints and a char in an NSArray.
Is there an easyer way than to declare an class holding the 2 ints and the char?
I tryed it this way and it works, but it seems rather complicated to me. Is there a better and easyer way?
@interface Container : NSObject
@property NSInteger a;
@property NSInteger b;
@property char c;
@end
@implementation Container
@synthesize a = _a;
@synthesize b = _b;
@synthesize c = _c;
-(Container*) initWitha:(NSInteger) a andB:(NSInteger) b andC: (char) c
{
if ((self = [super init])) {
self.a = a;
self.b = b;
self.c = c;
}
return self;
}
@end
...
//usage
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: [[Container alloc] initWitha:5 andB:6 andC:'D']];
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许你可以只使用 C 结构?
那么你可以做类似的事情
Maybe you could just use a C struct ?
then you could do something like
Apple 建议您不要在为
[super init]
赋值的同时,在构造函数中计算self
。您的 init 方法将需要阅读:
Apple recommends that you do not evaluate
self
in the constructors at the same time as assigning it[super init]
.Your init method will need to read: