从NSMutableArray中检索对象

时间:2012-10-14 11:26:41

标签: ios nsmutablearray

我有一个NSMutableArray,我想在另一个视图中使用它的数据。我读了,发现我们可以使用indexOfObject来读取数据,但我的NSMutableArray有多个值,我不知道如何从NSMutableArray获取特定字段。

任何帮助都会非常感激。

我目前卡在我的IBAction onStartPressed上。在

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

我显示了我涉及的其他文件和代码。

ClueData.h     #import

@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];


}

1 个答案:

答案 0 :(得分:1)

在IBAction中,首先将其保存/保存到正确的var,如

ClueData * sampleData = [ds objectAtIndex:0];

然后self.clueHint.text = [sampleData initialClue];

例如