Objective-C多维数组 [英] objective-c multi-dimensional array
问题描述
我想在我的应用程序中使用可变大小的多维数组来保存数据.我要使用的数据结构如下,每行的第一个元素后面是对应的多个值.
I want to use a variable-sized multi-dimensional array in my app to save data. The data structure I want to use is as below, the first element in each row is followed by corresponding multiple values.
array = { {a, a_val1, a_val2, a_val3}.
{b, b_val1},
{c, c_val1, c_val2, c_val3, c_val4, c_val5}
}
有什么想法可以在Objective-c中实现吗?
Any idea how I can implement in objective-c?
推荐答案
Objective-C没有真正的二维数组类型,但是您可以使用 以下代码.
Objective-C does not have a real 2 dimensional array type but you can implement it with the following codes..
在头文件中--- yourheader.h
in your header file --- yourheader.h
#import <Foundation/Foundation.h>
@interface yourheader : NSObject{
NSMutableDictionary* DictionaryArrayType;
NSMutableArray* MultiArrayType;
NSArray* CaptionTitle;
NSArray* ObjectValue;
}
@property (strong, nonatomic) NSMutableDictionary* DictionaryArrayType;
@property (strong, nonatomic) NSArray* CaptionTitle;
@property (strong, nonatomic) NSArray* ObjectValue;
@property (strong, nonatomic) NSMutableArray* MultiArrayType;
-(id) AddArrayObjects:(NSString*)_Name : (NSString*)_Surname :(NSString*)_Age;
-(id) AddArrayDictionaryObject:(NSArray*)_ArrayObject : (NSArray*)_ArrayKey;
-(id) AddMultiArrayType:(id)_ArrayObject;
-(void) ShowMultiArrayType:(id)_ArrayObject;
@end
现在添加到您的Objective-C文件中---- Objective-c.m
Now add to your objective-c file ---- objective-c.m
#import "yourheader.h"
@implimentation yourheader
@synthesize DictionaryArrayType;
@synthesize CaptionTitle;
@synthesize ObjectValue;
@synthesize MultiArrayType;
-(id)init {
if(self = [super init]){
NSString* const NAME = @"NAME";
NSString* const SURNAME = @"SURNAME";
NSString* const AGE = @"AGE";
//Adding fixed content to CaptionTitle Array
[self setCaptionTitle:[NSArray arrayWithObjects:NAME, SURNAME, AGE, nil]];
//add values to ObjectValue array
[self AddArrayObjects:@"Bob" :@"Obi" :@"200"];
//add values to dictionary
[self AddDictionaryArrayType:ObjectValue :CaptionTitle];
//Add to the Multi dimensional array [][]
[self AddMultiArrayType:DictionaryArrayType];
//add the second row values to ObjectValue array
[self AddArrayObjects:@"Barack" :@"Obama" :@"50"];
//add values to dictionary
[self AddDictionaryArrayType:ObjectValue :CaptionTitle];
//Add to the Multi dimensional array [][]
[self AddMultiArrayType:DictionaryArrayType];
//display the 2d Array
[self ShowMultiArrayType:MultiArrayType];
}
return self;
}
-(id)AddArrayObjects:(NSString *)_name :(NSString *)_surname :(NSString *)_age {
//Set the Array Objects;
[self setObjectValue:[NSArray arrayWithObjects:_name, _surname, _age, nil]];
return self;
}
-(id)AddDictionaryArrayType:(NSArray *)_ArrayObject :(NSArray*)_ArrayKey {
if(!DictionaryArrayType) {
//initialize disctionary
[self setDictionaryArrayType:[NSMutableDictionary dictionary]];
}
//add array obeject and Fixed Key decleared in CaptionTitle array
DictionaryArrayType = [NSMutableDictionary dictionaryWithObjects:_ArrayObject forKeys:_ArrayKey];
return self;
}
-(id) AddMultiArrayType:(id)_ArrayObject {
if(!MultiArrayType) {
[self setMultiArrayType:[NSMutableArray array]];
}
[MultiArrayType addObject:_ArrayObject];
return self;
}
-(void)ShowMultiArrayType:_ArrayObject {
for(id objects in _ArrayObject ) {
for(id key in objects) {
NSLog(@"%@ key = : object = %@", key, [objects objectForKey:key]);
}
}
}
@end;
最后,将其添加到应用程序内的appdelegate.m文件中
To finish add this to your appdelegate.m file inside the app
yourclassname* _yourclasspointer = [[yourclassname alloc] init];
[_youclasspointer ShowMultiArrayType:[_yourclasspointer MultiArrayType]];
您应该在控制台中看到它.
You should see it in you console.
这篇关于Objective-C多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!