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

Iphone 在NSArray中存储2个整数的元组和一个字符,,iphone,objective-c,ios,nsarray,Iphone,Objective C,Ios,Nsarray,我想在NSArray中存储2个整数的元组和一个字符。 有没有比声明一个包含2个整数和字符的类更简单的方法 我试着这样做,它的工作,但它似乎相当复杂的我。有更好更简单的方法吗 @interface Container : NSObject @property NSInteger a; @property NSInteger b; @property char c; @end @implementation Container @synthesize a = _a; @synthe

我想在NSArray中存储2个整数的元组和一个字符。 有没有比声明一个包含2个整数和字符的类更简单的方法

我试着这样做,它的工作,但它似乎相当复杂的我。有更好更简单的方法吗

@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']];

谢谢

苹果建议您不要在分配构造函数的同时在构造函数中评估
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;
}

也许你可以用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];

如果((super=[super init]))NSArray只能保存对象,那么您的init方法将需要
if((super=[super init]))
NSArray最好是创建一个C数组。您是否碰巧有一个链接,指向它在文档中的位置?给出了一个与您的陈述相矛盾的示例。如果您想应用良好的实践,那么保护比嵌套更可取:
if(!(self=[super init]){return nil;}\u a=a_b=b_c=c;回归自我。此外,ObjC中的方法名应该是
initWithA:b:c: