ios - Check if NSMutableArray contains an int -
i making app asks user series of questions. question asked depends on random int
produced. when int
used, want add together nsmutablearray
, , check if array contains number next time random number chosen. using next code this:
- (void) selectquestionnumber { textnum = lowerbounds + arc4random() % (upperbounds - lowerbounds); if ([previousquestions containsobject:[nsnumber numberwithint:textnum]]) { [self selectquestionnumber]; nslog(@"the same question number appeared!"); } else { questionlabel.text = [self nextquestion]; [self questiontitlechange]; nslog(@"new question made"); } [previousquestions addobject:[nsnumber numberwithint:textnum]]; }
however, code nslog(@"the same question number appeared!");
never shown in console, when same question appear twice.
my code non-functional, code can utilize check if nsmutable array contains int
?
original solution (works array , set):
-(void)selectquestionnumber { textnum = lowerbounds + arc4random() % (upperbounds - lowerbounds); nspredicate *predicate = [nspredicate predicatewithformat:@"intvalue=%i",textnum]; nsarray *filteredarray = [previousquestions filteredarrayusingpredicate:predicate]; if ([filteredarray count]) { [self selectquestionnumber]; nslog(@"the same question number appeared!"); } else { questionlabel.text = [self nextquestion]; [self questiontitlechange]; nslog(@"new question made"); } [previousquestions addobject:[nsnumber numberwithint:textnum]]; }
best solution, , improve performance, especialy mutableset ( according duncan c).
-(void)selectquestionnumber { textnum = lowerbounds + arc4random() % (upperbounds - lowerbounds); if ([previousquestions containsobject:[nsnumber numberwithinteger:textnum]]) { [self selectquestionnumber]; nslog(@"the same question number appeared!"); } else { questionlabel.text = [self nextquestion]; [self questiontitlechange]; nslog(@"new question made"); // , add together new number mutableset of mutablearray. [previousquestions addobject:[nsnumber numberwithinteger:textnum]]; } }
ios objective-c arrays nsmutablearray arc4random
No comments:
Post a Comment