mic*_*ica 2 iphone objective-c nsarray ios
我想存储2个int的元组和NSArray中的char.有没有比宣布持有2个整数和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']];
Run Code Online (Sandbox Code Playgroud)
谢谢
也许你可以使用C结构?
struct Container {
NSInteger a; // If you're using char c, why not use int a?
NSInteger b;
char c;
};
Run Code Online (Sandbox Code Playgroud)
然后你可以做点什么
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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2549 次 |
| 最近记录: |