在 NSArray 中存储 2 个 int 和一个 char 的元组,

发布于 2025-01-03 19:04:04 字数 690 浏览 1 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2025-01-10 19:04:04

也许你可以只使用 C 结构?

struct Container {
    NSInteger a; // If you're using char c, why not use int a?
    NSInteger b;
    char c;
};

那么你可以做类似的事情

struct Container c;

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:1]

// Insert
[array addObject:[NSValue value:&c withObjCType:@encode(struct Container)]];

// Retrieve:
struct Container c;
[[array objectAtIndex:i] getValue:&c];

Maybe you could just use a C struct ?

struct Container {
    NSInteger a; // If you're using char c, why not use int a?
    NSInteger b;
    char c;
};

then you could do something like

struct Container c;

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:1]

// Insert
[array addObject:[NSValue value:&c withObjCType:@encode(struct Container)]];

// Retrieve:
struct Container c;
[[array objectAtIndex:i] getValue:&c];
街角卖回忆 2025-01-10 19:04:04

Apple 建议您不要在为 [super init] 赋值的同时,在构造函数中计算 self

您的 init 方法将需要阅读:

-(Container *) initWitha:(NSInteger) a andB:(NSInteger) b andC: (char) c
{ 
    self = [super init];

    if ( self != nil ) {
        self.a =a;
        self.b=b;
        self.c=c;
    }

    return self;
}

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:

-(Container *) initWitha:(NSInteger) a andB:(NSInteger) b andC: (char) c
{ 
    self = [super init];

    if ( self != nil ) {
        self.a =a;
        self.b=b;
        self.c=c;
    }

    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文