gpt4 book ai didi

iphone - 在 NSArray 中存储 2 个整数和一个字符的元组,

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:07 26 4
gpt4 key购买 nike

我想在 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']];

谢谢

最佳答案

也许您可以只使用 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];

关于iphone - 在 NSArray 中存储 2 个整数和一个字符的元组,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com