UILabel does not update each time with for loop - ios

I'm trying to animate my label in my UICollectionViewCell. I want to go randomly through the array and set the label randomly 10 times and then it stops at a random string from my array.
The problem is, it goes through my array (debugged it with a simple NSLog) BUT the label setText isn't used till the last number of my loop and the label is then set.
Here is my code:
-(void)animate{
long index;
for(detail* cells in [[self col] visibleCells]){
NSIndexPath *indexPath = [[self col] indexPathForCell:cells];
if ([array count] >= 3) {
index = 2 * indexPath.section + indexPath.row;
}else{
index = 1 * indexPath.section + indexPath.row;
}
for (int i = 0; i <= 10; i++) {
[[cells detailLabel] setText: [[array objectAtIndex:index] returnRandomOptie]];
[NSThread sleepForTimeInterval:0.10];
NSLog(#"%i", i);
}
}
}
And here is my returnRandomOptie from my custom Cell class:
-(NSString *) returnRandomOptie {
NSUInteger randomIndex;
NSString *string;
if ([opties count] != 0) {
randomIndex = arc4random() % [opties count];
string = [NSString stringWithFormat:#"%#", [opties objectAtIndex:randomIndex]];
}
return string;
}
So basically I want that the for loop in my animate method, always sets the text in my label. And now it doesn't do that. Just at the last loop.
What am I doing wrong?
Kind Regards!

You have to perform the sleep async and update the label in the main thread, try this instead :
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i <= 10; i++) {
dispatch_async(dispatch_get_main_queue(), ^{
[[cells detailLabel] setText: [[array objectAtIndex:index] returnRandomOptie]];
});
[NSThread sleepForTimeInterval:0.10];
NSLog(#"%i", i);
}
});

Try this,
-(void)animate{
....
for (int i = 0; i <= 10; i++) {
dispatch_async(dispatch_get_main_queue(), ^{
[[cells detailLabel] setText: [[array objectAtIndex:index] returnRandomOptie]];
});
[NSThread sleepForTimeInterval:0.10];
NSLog(#"%i", i);
}
...
}

Related

IOS/Objective-C: NSArray randomization issue

I have this array with strings and I want to randomize it and display the output in a label. But with this code I'm having always the same strange output which is "2"...Any ideas what I'm doing wrong?
- (void)viewDidLoad {
[super viewDidLoad];
self.quoteInput.delegate=self;
self.quoteLabel=_quotationLabel;
self.correctLabel.alpha=0;
self.wrongLabel.alpha=0;
_quoteInput.delegate=self;
_levelOne = #[
[words Quotes:#"First Quote."],
[words Quotes:#"Second Quote."],
[words Quotes:#"And so on."]];
}
-(void)randomQuotes{
for (NSInteger x = 0; x < [_levelOne count]; x++) {
NSInteger randInt = arc4random() % ([_levelOne count] - x) + x;
[_levelOne objectAtIndex:randInt];
NSString *one = [NSString stringWithFormat:#"%d", randInt];
self.quoteLabel.text = one;
}
}
- (IBAction)generateQuote:(UIButton *)sender {
[self randomQuotes];
}
EDIT
The words class:
+ (instancetype)Quotes:(NSString *)quotes
{
return [[words alloc] initWithQuotes:quotes];
}
Unless you can explain what [word Quotes:] method does, it should be much simpler than what you are trying to do.
_levelOne = #[#"First Quote.", "Second Quote.", #"And so on."];
-(void) randomQuotes{
NSInteger randInt = arc4random_uniform( [_levelOne count] );
self.quoteLabel.text = [_levelOne objectAtIndex:randInt];
}

*** Collection <__NSArrayM: 0x117d7320> was mutated while being enumerated.'

I made a bug when I implement the search function. I opened an asynchronous thread. But when deleting a character (a digit of a phone number), the app would crash.
Error:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x117d7320> was mutated while being enumerated.'
Code:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//
self.isSearch = YES;
//remove the last search all the contacts
[self.resultArr removeAllObjects];
//
[self.rcs_SearchTableView reloadData];
//
dispatch_queue_t uploadQueue = dispatch_get_global_queue(0, 0);
dispatch_queue_t getMainQueue = dispatch_get_main_queue();
dispatch_async(uploadQueue, ^{
NSMutableArray *phoneArr = (NSMutableArray *)[self rcs_GetPhoneNumberFromeDatabaseWithPhone:searchText];
//
//NSLog(#"清空上次搜索的数据:%#", self.resultArr);
//NSLog(#"输入的关键字是---%#---%lu",searchText,(unsigned long)searchText.length);
if (0 == searchText.length || [searchText isEqualToString:#" "]) {
self.isSearch = NO;
//[self.rcs_SearchTableView reloadData];
[self.resultArr removeAllObjects];
}
//[self.rcs_SearchTableView reloadData];
if (0 != phoneArr.count) {
//
for (NSUInteger i = 0; i < phoneArr.count; i ++) {
RCSPhoneModel *flagPhoneModel = phoneArr[i];
for (NSUInteger i = 0; i < self.rcsRecentSearchDataSource.count; i ++) {
RCSContactModel *flagModel = self.rcsRecentSearchDataSource[i];
if ([flagPhoneModel.serverId isEqualToString:flagModel.serverId] || [flagPhoneModel.phone isEqualToString:flagModel.name]) {
//the same contact has multiple Numbers To prevent repeated add the same contacts
if (![self.resultArr containsObject:flagModel]) {
[self.resultArr addObject:flagModel];
continue;
}
}
}
}
}else{
//search contacts by name
for (NSInteger i = 0; i < self.rcsRecentSearchDataSource.count; i ++) {
RCSContactModel *model = self.rcsRecentSearchDataSource[i];
NSString *nameStr = model.name;
if (nameStr.length >= searchText.length) {
//search all the name
if ([nameStr containsString:searchText]) {
[self.resultArr addObject:model];
}
}
}
}
//
if (self.resultArr.count > 0) {
self.isSearch = YES;
//[self.rcs_SearchTableView reloadData];
}
//The phone contacts or local contact synchronized to the server
dispatch_async(getMainQueue, ^{
[self.rcs_SearchTableView reloadData];
});
});
}
A for loop should not enumerate anything that could change on any other thread or that could change within that loop. You should only enumerate an object that you are certain is not going to change while being enumerated (either in another thread, or within the loop itself). One way to do this is to only use a local copy of the array to enumerate over.
I can't see where anything being enumerated in your for loops is changed within the loop, so I would guess that in some other code in some other thread, you are changing either self.rcsRecentSearchDataSource or phoneArr. This crashes the for loop that enumerates self.rcsRecentSearchDataSource or phoneArr because it is required to not change while being enumerated.
Does this really need to be run on a separate thread?
If so, use a thread-local copy of the array to enumerate over, instead of the original array. That way you can be sure that nothing else can modify it, because it does not exist in any other scope.
Eg, there are two places where you could change your code to:
NSArray *localSearchDataSource = [self.rcsRecentSearchDataSource copy];
for (NSUInteger i = 0; i < localSearchDataSource.count; i ++) {
and one place where you could change to:
NSArray *localPhoneArr = [[self rcs_GetPhoneNumberFromeDatabaseWithPhone:searchText] copy];
for (NSUInteger i = 0; i < localPhoneArr .count; i ++) {
I got it answer and like unders codes:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//
self.isSearch = YES;
//
if (0 == searchText.length || [searchText isEqualToString:#" "]) {
self.isSearch = NO;
//[self.resultArr removeAllObjects];
}
//Remove the last search all the contacts
[self.resultArr removeAllObjects];
//
[self.rcs_SearchTableView reloadData];
//
NSMutableArray *localSearchDataSource = [self.resultArr mutableCopy];
//Create an array of objects as well as the original array
//According to the input access to the phone number of the data
NSArray *localPhoneArr = [[self rcs_GetPhoneNumberFromeDatabaseWithPhone:searchText] copy];
//
//[self.rcs_SearchTableView reloadData];
if (0 != localPhoneArr.count) {
//Because when data matching number takes longer, using asynchronous thread
dispatch_queue_t uploadQueue = dispatch_get_global_queue(0, 0);
dispatch_queue_t getMainQueue = dispatch_get_main_queue();
dispatch_async(uploadQueue, ^{
//
for (NSUInteger i = 0; i < localPhoneArr.count; i ++) {
RCSPhoneModel *flagPhoneModel = localPhoneArr[i];
for (NSUInteger i = 0; i < self.rcsRecentSearchDataSource.count; i ++) {
RCSContactModel *flagModel = self.rcsRecentSearchDataSource[i];
if ([flagPhoneModel.serverId isEqualToString:flagModel.serverId] || [flagPhoneModel.phone isEqualToString:flagModel.name]) {
//The same contact has multiple Numbers To prevent repeated add the same contacts
if (![localSearchDataSource containsObject:flagModel]) {
[localSearchDataSource addObject:flagModel];
}
}
}
}
//Add the search results to the search data source
dispatch_async(getMainQueue, ^{
[self.resultArr addObjectsFromArray:localSearchDataSource];
[self.rcs_SearchTableView reloadData];
});
});
}else{
//Search contacts by name
for (NSInteger i = 0; i < self.rcsRecentSearchDataSource.count; i ++) {
RCSContactModel *model = self.rcsRecentSearchDataSource[i];
NSString *nameStr = model.name;
if (nameStr.length >= searchText.length) {
//Search all name
if ([nameStr containsString:searchText]) {
[self.resultArr addObject:model];
}
}
}
}
//
if (self.resultArr.count > 0) {
self.isSearch = YES;
[self.rcs_SearchTableView reloadData];
}
}
I made two change that 'NSMutableArray *localSearchDataSource = [self.resultArr mutableCopy];' and 'NSArray *localPhoneArr = [[self rcs_GetPhoneNumberFromeDatabaseWithPhone:searchText] copy];'. And finish it . Collection <__NSArrayM: 0x117d7320> was mutated while being enumerated.'

Executes same code more than once. How to use Goto statement inside for loop

I have code which is used more than two times in different condition in same function. So I decided to use Goto statement. But that code will be executed inside for loop. So I don't understand how to call same code in same function. I don't want to create one more function. My code is...
- (void)setSelectedSearchCriteria:(NSString *)storedValue storedTag:(NSString *)storedTag D_Key:(NSString *)D_Key D_Tag_Value:(NSString *)D_Tag_Value arrayMain:(NSMutableArray *)arrayMain bgView:(UIView *)bgView
{
//Add data
NSMutableArray *sArray = [[storedValue componentsSeparatedByString:#","] mutableCopy];
NSMutableArray *sTagArray = [[storedTag componentsSeparatedByString:#","] mutableCopy];
[sArray removeObject:#""];
[sTagArray removeObject:#""];
int maxTag = 0;
if (sTagArray.count != 0)
{
maxTag = [[sTagArray valueForKeyPath:#"#max.intValue"] intValue];
for (int i = maxTag + 1; i <= [D_Tag_Value intValue]; i++)
goto add_value;
}
else
goto add_value;
add_value:
{
NSString *D_Value = [[arrayMain objectAtIndex:[D_Tag_Value intValue]] valueForKey:PARAMETER_KEY];
if (![sArray containsObject:D_Value])
{
[sArray addObject:D_Value];
[sTagArray addObject:D_Tag_Value];
}
//Add data
UIButton *btn = (UIButton *)[bgView viewWithTag:[D_Tag_Value intValue]];
[self setSelectedButtonStyle:btn];
}
storedValue = [[[sArray valueForKey:KEY_DESCRIPTION] componentsJoinedByString:#","] mutableCopy];
storedTag = [[[sTagArray valueForKey:KEY_DESCRIPTION] componentsJoinedByString:#","] mutableCopy];
[SEARCH_CRITERIAS setValue:storedValue forKey:D_Key];
[SEARCH_CRITERIAS_TAG setValue:storedTag forKey:D_Key];
}
Code inside add_value executed in for loop and also in else part. So I don't know how to manage this.
Define a block inside your function
void(^theBlock)(void) = ^(){
NSString *D_Value = [[arrayMain objectAtIndex:[D_Tag_Value intValue]] valueForKey:PARAMETER_KEY];
if (![sArray containsObject:D_Value])
{
[sArray addObject:D_Value];
[sTagArray addObject:D_Tag_Value];
}
//Add data
UIButton *btn = (UIButton *)[bgView viewWithTag:[D_Tag_Value intValue]];
[self setSelectedButtonStyle:btn];
};
I don't fully understand what do you do in your add_value. If it can change to a block receive some parameters and return some value that would be better
after that you simply call the block
theBlock();
The code doesn't actually depend on the loop counter, so it isn't too hard to refactor the code so that you can simply execute the loop the appropriate number of times.
- (void)setSelectedSearchCriteria:(NSString *)storedValue storedTag:(NSString *)storedTag D_Key:(NSString *)D_Key D_Tag_Value:(NSString *)D_Tag_Value arrayMain:(NSMutableArray *)arrayMain bgView:(UIView *)bgView
{
//Add data
NSMutableArray *sArray = [[storedValue componentsSeparatedByString:#","] mutableCopy];
NSMutableArray *sTagArray = [[storedTag componentsSeparatedByString:#","] mutableCopy];
[sArray removeObject:#""];
[sTagArray removeObject:#""];
int loopCount = 1;
if (sTagArray.count != 0) {
int maxTag = [[sTagArray valueForKeyPath:#"#max.intValue"] intValue];
loopCount = [D_Tag_Value intValue] - maxTag;
}
for (int i = 0; i < loopCount ; i++) {
NSString *D_Value = [[arrayMain objectAtIndex:[D_Tag_Value intValue]] valueForKey:PARAMETER_KEY];
if (![sArray containsObject:D_Value])
{
[sArray addObject:D_Value];
[sTagArray addObject:D_Tag_Value];
}
//Add data
UIButton *btn = (UIButton *)[bgView viewWithTag:[D_Tag_Value intValue]];
[self setSelectedButtonStyle:btn];
}
storedValue = [[[sArray valueForKey:KEY_DESCRIPTION] componentsJoinedByString:#","] mutableCopy];
storedTag = [[[sTagArray valueForKey:KEY_DESCRIPTION] componentsJoinedByString:#","] mutableCopy];
[SEARCH_CRITERIAS setValue:storedValue forKey:D_Key];
[SEARCH_CRITERIAS_TAG setValue:storedTag forKey:D_Key];
}

Randomizing position of correct answer in a quiz application for ios(Objective -C)

I am trying to develop a simple quiz application for IOS. I need to pick up 10 random images out of 50 , and display each image and 4 options with one correct answer. Presently I am able to randomize images , but each time, the correct option is displayed only at one position. Can anyone help me for randomizing the position of correct answer for different questions (i.e, images)?
- (void)viewDidAppear:(BOOL)animated
{
myData = nil;
[super viewDidAppear:animated];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"WoodTexture"]]];
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
myData = [[NSMutableArray alloc] initWithContentsOfFile:bundle];
[self setImageswithOptions];
count = 0;
score = 0;
[self resetScreen];
}
The method definition goes below.
- (void)setImageswithOptions
{
NSInteger number[4],i;
for ( i = 0; i <4 ; ++i ) {
number[i] = arc4random() % myData.count ;
for (int j = 0; j < i; ++j) {
if ((number[i] < 0) || (number[i] == number[j] )) {
--i;
}
}
}
self.submit.hidden =YES;
self.displayImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",myData[number[0]]]];
NSInteger random = arc4random() % 4;
if (random == 0) {
random += 1;
}
[(UILabel *)[self.view viewWithTag:random] setText:[NSString stringWithFormat:#"%#",myData[number[0]]]];
[(WQZOptionButton *)[self.view viewWithTag:random + 4] setIsCorrect:YES];
NSInteger imageCount = 0;
for (NSInteger index = 1; index <= 4; index++) {
if (index != random) {
imageCount++;
[(UILabel *)[self.view viewWithTag:index] setText:[NSString stringWithFormat:#"%#",myData[number[imageCount]]]];
[(WQZOptionButton *)[self.view viewWithTag:index + 4] setIsCorrect:NO];
}
}
[myData removeObjectAtIndex:number[0]];
self.scoreLabel.text = [NSString stringWithFormat:#"Score: %ld", (long)score];
}
There are several problems.
One problem:
NSInteger random = arc4random() % 4;
if (random == 0) {
random += 1;
}
random will be 1, 2 or 3 and 1 will occur twice as often as 2 or 3.
Also it is better to use arc4random_uniform() than the modular operator:
Instead of arc4random() % 4
use arc4random_uniform(4)

iOS: Setting variables on several custom view objects

Okay, so I'm creating X number of a custom UIView, that I've created in IB...
I create them in a grid-like formation and need to set their individual properties based on a response from a web service call...
The part I'm having trouble with is how to iterate through the different UIViews and set the variables...
I'm pretty sure the solution is really simple, but I've been staring blindly at this for some time now...
It's the part after:
if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
{
Here is the entire method:
- (void)receivedGroups
{
int rows, columns;
if([groupConnection.groupsArray count] <= 4)
{
rows = 1;
columns = [groupConnection.groupsArray count];
} else if([groupConnection.groupsArray count] >= 5 && [groupConnection.groupsArray count] <= 8)
{
rows = 2;
columns = ([groupConnection.groupsArray count] + 1 )/ 2;
} else
{
rows = 3;
columns = ([groupConnection.groupsArray count] + 2 )/ 3;
}
int number = 0;
for(int j=1; j < columns+1; j++)
{
for(int k=0; k < rows; k++)
{
// Only create the number of groups that match the number of entries in our array
if(number < [groupConnection.groupsArray count])
{
// Create an instance of the group view
GroupEntry *groupEntry = [[GroupEntry alloc] initWithFrame:CGRectMake(230*j, 250*k, 180, 233)];
// Add it to the view
[self.view addSubview:groupEntry];
// Get the group
GetGroupsActive *theGroups = [groupConnection.groupsArray objectAtIndex:number];
groupEntry.groupNameLabel.text = theGroups.groupName;
for(int i=0; i<[statusConnection.statusArray count]; i++)
{
CurrentStatus *theStatus = [statusConnection.statusArray objectAtIndex:i];
if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
{
//allChildren++;
switch(theStatus.currentStatus)
{
case 0:
//childrenSick++;
break;
case 1:
//childrenVacation++;
break;
case 2:
//childrenPresent++;
break;
case 3:
//childrenOut++;
break;
case 4:
//childrenTour++;
break;
default:
break;
}
}
}
NSString *allLabelText = [NSString stringWithFormat:#"%i", allChildren];
NSString *sickLabelText = [NSString stringWithFormat:#"%i", childrenSick];
NSString *vacationLabelText = [NSString stringWithFormat:#"%i", childrenVacation];
NSString *presentLabelText = [NSString stringWithFormat:#"%i", childrenPresent];
NSString *outLabelText = [NSString stringWithFormat:#"%i", childrenOut];
NSString *tripLabelText = [NSString stringWithFormat:#"%i", childrenTour];
groupEntry.sickLabelNumber.text = sickLabelText;
groupEntry.presentLabelNumber.text = presentLabelText;
groupEntry.numberLabelNumber.text = allLabelText;
groupEntry.tripLabelNumber.text = tripLabelText;
groupEntry.outLabelNumber.text = outLabelText;
groupEntry.vacationLabelNumber.text = vacationLabelText;
// Create the buttons to handle button press
UIButton *childButton = [UIButton buttonWithType:UIButtonTypeCustom];
childButton.frame = CGRectMake(230*j, 250*k, 180, 233);
// Set an identity tag, so we can recognize it during button press
childButton.tag = theGroups.ID;
// When EventTouchUpInside, send an action to groupSelected:
[childButton addTarget:self action:#selector(groupSelected:) forControlEvents:UIControlEventTouchUpInside];
// Add it to the view
[self.view addSubview:childButton];
}
number++;
}
}
}
If you added all the views in a parent view. You can get all the subviews using,
NSAarry *subviews = [base subviews];
for(UIView *subview in subviews)
{
subview.property = yourVaule;
}
You can differentiate between subviews using its tag or another property.

Resources