2012-10-14 106 views
0

我有一個NSMutableArray,我想在另一個視圖中使用它的數據。我讀了,發現我們可以使用indexOfObject讀取數據,但我的NSMutableArray有多個值,我不知道如何從NSMutableArray獲得特定的字段。從NSMutableArray檢索對象

任何幫助將不勝感激。

我目前被困在我的IBAction onStartPressed。在線

self.clueHint.text = ds[0].initialClue; 
//or 
    self.clueHint.text = ds objectAtIndex:0 initialClue; 

我顯示了我涉及的其他文件和代碼。

ClueData.h #進口

@interface ClueData : NSObject 
@property(nonatomic, strong) NSString * clueName; 
@property(nonatomic, strong) NSString * initialClue; 
@property(nonatomic, strong) NSString * clueHint1; 
@property(nonatomic, strong) NSString * clueHint2; 

@property(nonatomic, strong) NSString * clueAnswer; 
@property(nonatomic, strong) UIImage * clueImage; 
@end 

ClueDataDAO.h

#import <Foundation/Foundation.h> 
#import "ClueData.h" 
@interface ClueDataDAO : NSObject 
@property(strong, nonatomic) NSMutableArray * clueDataArray; 

-(NSMutableArray *) PopulateDataSource; 
@end 

ClueDataDAO.m

#import "ClueDataDAO.h" 

@implementation ClueDataDAO 
@synthesize clueDataArray; 
-(NSMutableArray *) PopulateDataSource 
{ 
    clueDataArray = [[NSMutableArray alloc]init]; 
    ClueData * sampleData = [[ClueData alloc]init]; 

    sampleData.clueName = @"Tropical Fruit Tree"; 
    sampleData.initialClue = @"Where all the Tropical trees are planted."; 
    sampleData.clueHint1 = @"It is located at Area 10"; 
    sampleData.clueHint2 = @"It could be found near Eco Lake"; 
    sampleData.clueAnswer = @"Fruit tree collection"; 
    NSString * file=[[NSBundle mainBundle]pathForResource:@"FruitTreeCollection" ofType:@"jpg"]; 
    sampleData.clueImage = [UIImage imageWithContentsOfFile:file]; 
    [clueDataArray addObject:sampleData]; 
    sampleData=nil; 

    return clueDataArray; 
} 
@end 

GamePageViewController.M

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    daoDS = [[ClueDataDAO alloc]init]; 
    self.ds = daoDS.PopulateDataSource; //load pre-build NSMutableArray 
    self.answer.delegate = self; 
    // Do any additional setup after loading the view. 
} 

- (IBAction)onStartPressed:(id)sender { 

    //initiate load object 
// stuck at here :(

    ClueData * currentClue = [ds objectAtIndex:0]; // app got terminate at this line of code 
self.clueHint.text = [currentClue initialClue]; 


} 
在IBAction爲,第一鑄造/保存到正確的變種
+1

你是什麼意思「我的數組有多個字段」?那麼,這就是爲什麼它被稱爲一個數組而不是一個原始類型......此外,**使用空格。** @property(強,非原子)NSMutableArray * clueDataArray;'只是徹底傷害。同樣,這個問題**與Xcode沒有任何關係。** – 2012-10-14 11:32:14

+0

我認爲他的意思是:每個對象都有多個項目,所以NSDict的基礎知識或(自定義)類作爲數組的項目。 –

+0

對不起..我是一個noob ...仍然是新的... –

回答

1

,像

ClueData *的sampleData = [DS objectAtIndex:0];

then self.clueHint.text = [sampleData initialClue];

作爲示例

+0

我得到了「終止應用程序由於未捕獲異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空數組的界限'」這個錯誤當我添加行「ClueData * sampleData = [ds objectAtIndex:0];」 –

+0

然後你的數組是空的......所以打印你的數組。將數組作爲類變量,並確保在init類或viewDidLoad類中初始化數組,或者... –

+0

現在就可以工作。我在別的地方擦了一個錯誤數組...謝謝你jelle –