2012-10-14 76 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 –