0

編集:私のプロジェクトで使用されるすべての配列はNSMutableArrayクラスです

私がやりたいことの概要は私のselectClueViewからのもので、ユーザーは3から10までの数字を選択できます。彼らがプレイする手がかりの数を表します。次に、0 から の間の乱数のリストを生成し、dataArray として知られる別の配列にobjectArray.count追加します。どの転送先をNSNumber含め、すべてが正常に機能していますprepareForSegueSelectClueViewController.dataArrayGamePageViewController.clueToSelect

ただし、dsすべてのオブジェクトを保持する配列から、新しい配列にデータをロードすることに固執していますallDataObject。私はiOSにかなり慣れていません.C#で機能していたので、Objective-Cで複製しようとしましたが、残念ながら完全には複製できないようです.

allDataObjectつまり、配列のデータを配列dsNSNumber値で配列に追加しようとしています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");
    }
}

GamePageViewController.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数字の場合は試してみてください。

編集 :

NSNumbers のみを考慮cluesToSelectして含む、より読みやすいものを次に示します。allDataObject

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オブジェクトを からクラス オブジェクトcurrentClueDataにロードする新しい方法で機能しなかった理由は、クラス オブジェクトを に追加した後dsに機能しなかったためです。オブジェクトは、他の言語のように独自の値を上書きできませんでした。(それがおそらく、客観的な C でそれを行うために私の脳を破壊している理由です) しかし、オブジェクトを追加してキャストした後、今ではうまく機能しています。皆さんありがとう :)nilalloc initdsnilalloc and init

-(void)fillDataSample { int カウント = 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 に答える