0

编辑:我的项目中使用的所有数组都是NSMutableArray

我想要做的概述来自我的 selectClueView,用户可以从 3-10 中选择一个数字。这代表他们将播放的线索数。然后它将生成 0 和 之间的随机数列表,并将其objectArray.count添加NSNumber到另一个称为 dataArray 的数组中。一切正常,包括prepareForSegue转移SelectClueViewController.dataArrayGamePageViewController.clueToSelect

但是,我坚持将数据ds从包含所有对象的数组加载到新数组中allDataObject。我对 iOS 相当陌生,因为我在 c# 中有一个工作函数,所以我试图在 Objective-C 中复制它,不幸的是,我似乎无法完全复制它。

简而言之,我正在尝试将数组中的数据添加allDataObjectds具有数组NSNumber值的cluesToSelect数组中。

以下是使用的编码。任何解决此问题的帮助将不胜感激。如果我应该提供更多信息,请告诉我。

SelectClueViewController.m

- (IBAction)onGoPress:(id)sender {
    [self chooseNumber];
     NSLog(@"Array got %d numbers",dataArray.count);
}

-(void)chooseNumber
{
    [dataArray removeAllObjects];
    maxCount = [numberOfClues.text intValue];

    int count = 0;
    do {

        NSInteger rdmNumber = arc4random()%objectArray.count;
        if (![dataArray containsObject:[NSNumber numberWithInt:rdmNumber]])
        {
            NSNumber* number = [NSNumber numberWithInt:rdmNumber];
            [dataArray addObject:number];
            count++;
            NSLog(@"random no - %d",rdmNumber);
        }
    } while (count < maxCount);

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"sendNumber"]) {


        GamePageViewController *gpViewController = [segue destinationViewController];
        gpViewController.cluesToSelect = self.dataArray;
        NSLog(@"Success");
    }
}

游戏页面视图控制器.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    daoDS = [[ClueDataDAO alloc]init];
    self.allDataObject = daoDS.PopulateDataSource;
    NSLog(@"%d",cluesToSelect.count);

    [self fillDataSample];

    //for keyboard
    self.answer.delegate = self;        
}

    -(void)fillDataSample
{
    int count = 0;
    do {
       // [self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count]intValue] ]];
        ds = [[NSMutableArray alloc]init];
        currentClueData = [[ClueData alloc]init];        
        int firstIndex = [[cluesToSelect objectAtIndex:count]intValue];
        currentClueData = [allDataObject objectAtIndex:firstIndex];
        [ds addObject:currentClueData];

        count++;
    } while (count < cluesToSelect.count);
    NSLog(@"ds got %d object",ds.count);
}

编辑:

我现在可以让它将对象添加到ds数组中,不幸的是它只添加了一次。有人可以看看我的fillDataSample功能吗?

4

3 回答 3

0

在您指向开头的行中:

[self.ds addObject:[allDataObject objectAtIndex:[cluesToSelect objectAtIndex:count]]];

只是一个疯狂的猜测,但[cluesToSelect objectAtIndex:count]返回一个对象。你试图将它传递给另一个objectAtIndex:,它需要一个int作为参数。我猜错误可能来自于此。.intValue如果是数字,您可以尝试使用。

编辑 :

这是更具可读性的内容,考虑cluesToSelectallDataObject仅包含 NSNumbers :

int firstIndex = [[cluesToSelect objectAtIndex:count] intValue];
int secondIndex = [[allDataObject objectAtIndex: firstIndex] intValue];
[self.ds addObject:secondIndex];
于 2012-11-29T15:57:17.690 回答
0

根据我们的讨论,

请检查这个:

[self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count] intValue]]];

会做你的工作。

于 2012-11-29T15:59:57.780 回答
0

为什么它不适用于我将对象从类对象加载到类对象的新方法是allClueData因为我没有,并且将类对象添加到. 该对象无法像在其他语言中那样覆盖自己的值。(这可能就是为什么我在目标 C 中这样做而破坏我的大脑)但是在添加了 object 和 cast之后,它现在工作得很好。谢谢大家:)currentClueDatadsnilalloc initdsnilalloc and init

-(void)fillDataSample { int count = 0; currentClueData = [[ClueData alloc]init]; ds = [[NSMutableArray alloc]init]; do { // [self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count]intValue] ]];

    int firstIndex = [[cluesToSelect objectAtIndex:count]intValue];
    currentClueData = [allDataObject objectAtIndex:firstIndex];
    [ds addObject:currentClueData];
    currentClueData =nil;
    currentClueData = [[ClueData alloc]init];


    count++;
} while (count < cluesToSelect.count);
NSLog(@"ds got %d object",ds.count);

}

于 2012-12-01T02:38:37.107 回答