2012-11-29 26 views
0

编辑:在我的项目中使用的所有阵列NSMutableArray的iOS从另一个阵列基础上,第三阵列的增值对象数组

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

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

总之,我试图将allDataObject数组中的数据添加到ds数组中,其中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"); 
    } 
} 

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功能吗?

回答

0

的原因,为什么它没有为我的装载物的新方法,从allClueData工作类对象currentClueDatads是因为我没有nilalloc init类对象将其添加到ds后。该对象无法像使用其他语言一样覆盖自己的价值。(这可能是为什么我在破坏我的大脑的目标C中做到这一点)但是在nil中加入对象并铸造alloc 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); 

}

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]; 
+0

嗯,这是否意味着已经加入了阵列自动成为一个对象什么?即使我加入的是'NSNumber'?对不起,询问,我很新的iOS Objective-C –

+0

NSNumber是一个对象!我正在编辑我的帖子,为您提供您应该使用的线路。 – rdurand

+0

啊抱歉,当我问起时,凌晨1点在我身边。刚醒来。在清新之后要尝试一下 –

0

根据我们的讨论,

请检查这一个:

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

会做你的工作。

+0

对不起,但在新的可可风格中你是什么意思?这是我必须进口可可图书馆吗? –

+0

xcode4使用文字的数字...我不记得,我现在没有mac,否则我wud检查... :( –

+0

指这个... NSNumber * fortyTwo = @ 42; //相当于[NSNumber numberWithInt:42] 的NSNumber * fortyTwoUnsigned = @ 42U; //等价于[NSNumber的numberWithUnsignedInt:42U] 的NSNumber * fortyTwoLong = @ 42L; //等价于[NSNumber的numberWithLong:42L] 的NSNumber * fortyTwoLongLong = @ 42LL; //相当于[NSNumber numberWithLongLong:42LL] –

相关问题