还在学习,我正在尝试创建一个简单的多维数组,每个记录中有4条记录和4个值 - 一个字符串; 2x浮动和一个int。关于Objective-C的参考文档似乎是静音的,除非我被盲目或使用了错误的术语。
有什么想法吗? 问候, 布鲁克纳
答案 0 :(得分:0)
Objective C不包含连续多维数组的概念(如C中所示)。您可以使用@GrzegorzKrukowski建议使用数组数组,但是您可能会混淆索引和顺序。
以下是两个更好的解决方案:
NSDictionary
个对象的NSIndexPath
对象作为键来记录您的索引路径,例如[1, 2]
; 答案 1 :(得分:-1)
NSMutableArray *arr = [NSMutableArray arrayWithObjects:
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array], nil];
要添加int,浮动到任何数组使用:NSNumber numberWithInt:或numberWithFloat:
[[arr objectAtIndex:0] addObject:[NSNumber numberWithInt:10]];
获得价值
[[[arr objectAtIndex:0] objectAtIndex:0] intValue];
使用文字应该在这里缩短代码,但我使用完整的代码来澄清。
答案 2 :(得分:-1)
您可以使用以下代码
来实现此目的 NSMutableArray *arrFinal=[[NSMutableArray alloc]init];
NSMutableArray *arr=[[NSMutableArray alloc]init];
[arr addObject:@"String"];
[arr addObject:[NSString stringWithFormat:@"%f",floatNumber1]];
[arr addObject:[NSString stringWithFormat:@"%f",floatNumber1]];
[arr addObject:[NSString stringWithFormat:@"%d",intnumber1]];
[arrFinal addObject:arr];//add that array to final array.
arr=nil;
//If you want to access it then
NSMutableArray *arrRecord1=[arrFinal objectAtIndex:0];
而不是我的建议是创建一个客观c类,其父级为NSObject
,只是为了定义实体,如下所示
//in .h class
@interface UOMList : NSObject
{
int idUnits;
NSString *cUnitCode;
}
@property (nonatomic)int idUnits;
@property (nonatomic, retain)NSString *cUnitCode;
-(id)init;
-(void)dealloc;
@end
//in .m class
@implementation UOMList
@synthesize idUnits;
@synthesize cUnitCode;
-(id)init
{
if (self = [super init])
{
self.cUnitCode=@"";
idUnits=0;
}
return self;
}
//releaseContent method to release the content
-(void)releaseContent:(id)sender
{
[sender release];
sender=nil;
}
//dealloc method to release the content
-(void)dealloc
{
[self releaseContent:cUnitCode];
[super dealloc];
}
现在,无论何时需要在记录中为数据填充分配对象,并将其保存在数组中,如下所示
UOMList *obj1=[[UOMList alloc]init];
obj1.cUnitCode=@"String";
obj1.idUnits=intval;
[arrFinal addObject:obj1];
然后您可以按以下方式访问
UOMList *obj1=[arrFinal objectAtIndex:0];
第二种方法如此简单。我希望这会对你有所帮助。