2011-09-18 89 views
3

我想在我的應用程序中使用可變大小的多維數組來保存數據。我想要使​​用的數據結構如下所示,每行中的第一個元素後面跟着相應的多個值。objective-c多維數組

array = { {a, a_val1, a_val2, a_val3}. 
      {b, b_val1}, 
      {c, c_val1, c_val2, c_val3, c_val4, c_val5} 
     } 

任何想法如何在objective-c中實現?

回答

-1

的Objective-C沒有一個真正的2維數組類型,但是你可以用 以下代碼實現它..

在你的頭文件

--- 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的文件----客觀釐米

#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文件裏面的應用程序

yourclassname* _yourclasspointer = [[yourclassname alloc] init]; 

[_youclasspointer ShowMultiArrayType:[_yourclasspointer MultiArrayType]]; 

你應該在你的控制檯中看到它。

+1

您的解決方案忽略了客觀的c語言約定,例如以小寫字母開頭的屬性名稱,命名所有方法參數並始終使用聲明屬性的getter和setter方法 – jackslash

14

使用NSMutableArray裏,像這樣

NSMutableArray *curRow; /* use to access the row while loading with objects */ 
NSMutableArray *array = [[NSMutableArray alloc] init]; /* your main multidim array */ 
curRow = [NSMutableArray array]; 
[curRow addObject:/* what you want here */]; 
[curRow addObject:/* what you want here */]; 
[curRow addObject:/* what you want here */]; 
[array addObject:curRow]; /* first row is added */ 

/* rinse and repeat */ 
curRow = [NSMutableArray array]; 
[curRow addObject:/* what you want here */]; 
[curRow addObject:/* what you want here */]; 
[curRow addObject:/* what you want here */]; 
[array addObject:curRow]; 
+0

謝謝 - 這是完美的。我將這個標記爲我的答案,因爲它提供了一個通用的解決方案 - 用於在運行時創建動態數組。下一個答案顯示了固定大小數組的用法。 – Sam

+0

這個答案好多了 – michaelsnowden

+0

要調用一個特定的對象,你可以調用它所在的行(第一組「行」將是0,第二組是1等)。然後調用「addObject」(列數)它是...第一個「addObject」是「0」,第二個是「1」,等等 '[[array objectAtIndex:/ *行號* /] objectAtIndex:/ *列號* /] ;' –

5

使用NSMutableArray

下面是你的理解的例子...

NSMutableArray * multiArray = [[NSMutableArray alloc] initWithCapacity:5]; 
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2]]; 
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2,a_val3,a_val4]]; 
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val5]]; 
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2,a_val3,a_val4,a_val5,a_val6]]; 

不要忘記釋放到multiArray陣列因爲我們有alloced它...

+0

感謝您的回答....但我一直在尋找創建一個動態多線程的想法,運行時維數組。事先的答案是完美的。 – Sam

+0

@Sam爲什麼你認爲這種方法不能在運行時使用? –

+0

@AlbertRenshaw,這個答案類似於bigkm的,它可以在運行時使用。但是,它要求您提前添加子數組的對象(arrayWithObjects:...)。例如,如果你不知道最初在給定的子數組中有多少物體,那麼bigkm的答案允許創建對象並「即時」添加。 – leanne