当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - Genarate custom unique index from an NSMutuableArray ios

问题描述:

I am using arc4random() function for extracting the index of an NSMutableArray. My array count is 81. But the problem is my Simulator is hanging many times. For unique index, I am taking one NSMutableArray to store the random index if it satisfies the condition. For that I used containsObect method of NSMutableArray to check for existing indexes.

Please help me.

- (void)viewDidLoad

{

alphabetArray=[[NSMutableArray alloc]initWithObjects:@"rescue",@"under",@"special",@"physical",@"above",nil];

}

-(void)createButtonFrame

{

for (float colCount=1;colCount<222.6;) {

for (float rowCount=1;rowCount<=450;) {

alphabetButton=[[UIButton alloc]initWithFrame:CGRectMake(rowCount-1,colCount-1,50,27.4)];

alphabetButton.layer.borderColor=[UIColor grayColor].CGColor;

alphabetButton.layer.borderWidth = 0.8f;

alphabetButton.tag=tagButtonValue;

[masterView addSubview:alphabetButton];

[btnArray addObject:alphabetButton];

rowCount+=50;

tagButtonValue++;

}

colCount+=27.4;

}

-(void)puAlphabetFunction

{

for (int wordCount=0;wordCount<[alphabetArray count];) {

word=[alphabetArray objectAtIndex:wordCount];

[self putrandomAlphabet:word];

[alphabetArray removeObjectAtIndex:wordCount];

}

}

}

-(void)putrandomAlphabet:(NSString*)wordString

{

for (int characterCount=0;characterCount<wordString.length;) {

NSLog(@"%d",characterCount);

retreiveCharacter = [wordString substringWithRange:NSMakeRange(characterCount, 1)];

randomNumber = arc4random() % [btnArray count];

btnvalue=(UIButton *)[self.view viewWithTag:randomNumber+1];

if (firstTime==0) {

lastIndex=randomNumber;

duplicateIndex=randomNumber;

[btnvalue setTitle:retreiveCharacter forState:UIControlStateNormal];

firstTime++;

[indexArray addObject:[NSString stringWithFormat:@"%d",randomNumber]];

characterCount++;

[btnArray removeObjectAtIndex:randomNumber];

}

else

{

if([indexArray containsObject:[NSString stringWithFormat:@"%d",randomNumber]])

{

[btnArray removeObjectAtIndex:randomNumber];

}

else

{

lastIndex=randomNumber;

duplicateIndex=randomNumber;

[btnvalue setTitle:retreiveCharacter forState:UIControlStateNormal];

[indexArray addObject:[NSString stringWithFormat:@"%d",randomNumber]];

[btnvalue setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[btnvalue.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:17.0]];

}

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: