NSMutableArray shuffle issue - iphone

I have solved the problem with including terminating method.
If(indexes==0){
endUp = YES;
}
Thanks for pointing me the right direction. It wasn't problem of the shuffle.
Alessign

the error may be in the loop you use to shuffle the first array.
You don't post that part of your code...is it something like this?
for (int i=0; i<[indexes count]; i++){
// (...)
[indexes removeObjectAtIndex:index];
// (...)
}
this may be better:
int arrayCount = [indexes count];
for (int i=0; i<arrayCount; i++){
// (...)
[indexes removeObjectAtIndex:index];
// (...)
}
this complete code works well, with no errors or crashes:
int length = 10;
NSMutableArray* indexes = [[NSMutableArray alloc] initWithCapacity:length];
for (int i=0; i<10; i++) [indexes addObject:[NSNumber numberWithInt:i]];
NSMutableArray*shuffle = [[NSMutableArray alloc] initWithCapacity:length];
int arrayCount = [indexes count];
for (int i=0; i<arrayCount; i++){
int index = arc4random()%[indexes count];
NSLog(#"___index: %i", index);
NSLog(#"indexes: %# ", indexes);
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
NSLog(#"shuffle: %# ", shuffle);
}
for (int i=0; i<[shuffle count]; i++){
int questionNumber = [[shuffle objectAtIndex:i] intValue] + 1;
NSLog(#"questionNumber: %i ", questionNumber);
}

I see, anyway, Got it! I implemented one 'if' statement which will terminate it!
if ([indexes count] == 0)
{endProcess = YES}

Related

can't make addition in NSMutableArray obectatindex

for(int i=0; i<[arrMaintenanceDetail count]; i++){
for(int j=0; j<[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"] count]; j++){
if([[arrMaintenanceDetail objectAtIndex:i] isEqualToString:[[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"]objectAtIndex:i] stringValue]]){
[arrTotalValues objectAtIndex:i] += [arrTotalValues insertObject:[[[arrMaintenanceCategory valueForKey:#"cost"] objectAtIndex:j] integerValue] atIndex:i];
}
}
}
I want to add (make addition of integer values) value from
[[[arrMaintenanceCategory valueForKey:#"cost"] objectAtIndex:j] integerValue];
Whenever loop changes its value.
I want new value from objectAtIndex:j to be added to its previous value and store it for future use.
I will have 4 objects in arrTotalValues (NSMutableArray).
So what should I do ??
You cannot keep integers in NSMutableArray. Use NSNumber to keep integer values in your NSMutableArray as objects.
You didn't provide enough information to give you 100% working code, but try something like that:
NSMutableArray* arrTotalValues = [NSMutableArray array];
for(int i=0; i<[arrMaintenanceDetail count]; i++)
{
NSNumber* totalValue = [NSNumber numberWithInt:0];
for(int j=0; j<[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"] count]; j++)
{
if([[[arrMaintenanceDetail objectAtIndex:i] stringValue] isEqualToString:[[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"] objectAtIndex:i] stringValue]])
{
int newTotalValue = [totalValue intValue] + [[[arrMaintenanceCategory valueForKey:#"cost"] objectAtIndex:j] integerValue];
totalValue = [NSNumber numberWithInt:newTotalValue];
}
}
[arrTotalValues addObject:totalValue];
}

About NSMutableArray add NSArray issue

I want to below effect,but I don't kown how to use NSMutableArray combine NSArray More than two?
1.my code
for (int i=0; i<[DateSortArry2 count]; i++) {
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[Arry addObject:sectiondateStr2];
}
}
[SumArry addObjectsFromArray:Arry];
[Arry removeAllObjects];
}
2.my code Result
SumArry:(
"20130227",
"20130227",
"20130227",
"20130226",
"20130226",
"20130226",
"20130225",
"20130225")
3.I want the results
SumArry:((
"20130227",
"20130227",
"20130227",
),
(
"20130226",
"20130226",
"20130226",
),
(
"20130225",
"20130225"
))
Your code repeatedly fills and empties the same array by adding its elements, but you need to preserve the structure with additional instances of NSArray. So, use a new NSArray for each section.
for (int i=0; i<[DateSortArry2 count]; i++) {
NSMutableArray *section = [NSMutableArray array];
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[section addObject:sectiondateStr2];
}
}
[SumArry addObject:section];
}
You can either store a reference to another array (or any type of object) in your array:
[myMutableArray addObject:otherArray];
Or concatinate the arrays.
[myMutableArray addObjectsFromArray:otherArray];
Both of which are documented in the documentation. By the looks of it the first approach is what you want since you want to have NSArray of NSMutableArray.
try this:
please tell me if it works.
thanks
NSString *str = #"";
for (int i=0; i<[DateSortArry2 count]; i++)
{
if (str isEqualToString:[DateSortArry2 objectAtIndex:i])
{
return;
}
else
{
NSMutableArray * Arry = [[NSMutableArray alloc] init];
str = [DateSortArry2 objectAtIndex:i]
for (int j=0; j<[DateSortArry2Copy count]; j++)
{
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:str])
{
[Arry addObject:str];
}
}
[SumArry addObject:Arry];
[Arry removeAllObjects];
}
}

How to store index of a for loop in an array Xcode

I have a for loop running on string length. the first loop looks for a character common in the second loop. I want to save the index of nameString that is common in an array or a string. Please help. Code is mentioned below.
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
NSRange range;
range.length=1;
range.location=j;
[partnersNameString replaceCharactersInRange:range withString:#""];
calculateField.text = partnersNameString;
}
}
}
There are several ways to go about doing what you are looking for: you can use a "plain" C array with a count, or you can use an NSMutableArray with wrappers.
The first way would look like this:
NSUInteger indexes[MAX_LENGTH];
NSUInteger lastIndex = 0;
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
indexes[lastIndex++] = i;
// Go through the rest of your code
...
}
}
}
for (NSUInteger i = 0 ; i != lastIndex ; i++) {
NSLog(#"Found match at index %u", indexes[i]);
}
The second way looks similar, except now you need to use NSNumber to wrap the data going into NSMutableArray:
NSMutableArray *indexes = [NSMutableArray array];
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
[indexes addObject[NSNumber numberWithInt:i]];
// Go through the rest of your code
...
}
}
}
for (NSNumber in indexes) {
NSLog(#"Found match at index %i", [[indexes objectAtIndex:i] intValue]);
}
You can use NSMutablearray for that
NSMutableArray myArray = [NSMutableArray new];
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
//And add it here
[myArray addObject:currentLetter];
NSRange range;
range.length=1;
range.location=j;
[partnersNameString replaceCharactersInRange:range withString:#""];
calculateField.text = partnersNameString;
}
}
}
[myArray addObject:[NSNumber numberWithInt:i]];

Error when trying to shuffle an NSMutableArray

I'm trying to shuffle an NSMutableArray so that its order will be mixed up every-time someone loads the view.
In my -(void)viewDidLoad I'm putting the following code (as suggested by other users):
NSMutableArray *shuffleTwo = [self.chosenTeamDict objectForKey:#"clubs"];
int random = arc4random() % [shuffleTwo count];
for (int i = 0; i < [shuffleTwo count]; i++) {
[shuffleTwo exchangeObjectAtIndex:random withObjectAtIndex:i];
}
NSLog(#"%#", shuffleTwo);
But when I do this and try and run the page, I get the following error:
2012-07-09 18:42:16.126 Kit-Quiz[6505:907] (null)
libc++abi.dylib: terminate called throwing an exception
Can anyone advice either a new way of shuffling this array, or advice me on how to avoid this error..!? I'm building for iOS 5 and I'm using Xcode45-DP1. Thanks in advance!
(EDIT)
I've also tried this method and I get the same error:
NSMutableArray *shuffledArray = [[NSMutableArray alloc] init];
NSMutableArray *standardArray = [self.chosenTeamDict objectForKey:#"clubs"];
for(int s = 0; s < [standardArray count]; s++){
int random = arc4random() % s;
[shuffledArray addObject:[standardArray objectAtIndex:random]];
}
NSLog(#"%#", shuffledArray);
NSMutableArray *standardArray = [self.chosenTeamDict objectForKey:#"clubs"];
int length = 10; // int length = [yourArray count];
NSMutableArray *indexes = [[NSMutableArray alloc] initWithCapacity:length];
for (int i=0; i<10; i++) [indexes addObject:[shuffledArray objectAtIndex:i]];
NSMutableArray *shuffle = [[NSMutableArray alloc] initWithCapacity:length];
while ([indexes count])
{
int index = rand()%[indexes count];
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
}
for (int i=0; i<[shuffle count]; i++) NSLog(#"%#", [shuffle objectAtIndex:i]);
NSLog(#"%#", shuffle);
^^ ANSWER
Try Fisher-Yates shuffle. It goes like this:
int count = shuffledArray.count;
for(int i=count; i>0; i--) {
int j = arc4random_uniform(count);
[shuffledArray exchangeObjectAtIndex:j withObjectAtIndex:i];
}
make sure that your array is non-nil and all the entries are allocated objects :)
Source: Fisher-Yates Shuffle
First, you really should enable exception breakpoints. In XCode on the left-hand panel, click the breakpoint tab, click the "+" sign at the bottom-left -> exception breakpoint -> done.
I suspect your problem lies here:
int random = arc4random() % [shuffleTwo count];
If [shuffleTwo count] evaluates to zero (also if shuffleTwo is nil) it will throw a division by zero exception. Edit: Doesn't seem to be the case in Objective-C.

How to position the untitled section in a UITableView

At present if the sortdescriptor is having nil or empty values is being placed in an untitled section which is being placed at the top of the table. I want it to be at the end of the table. Any suggestions?
yes, it is so easy, jst perform a segmentation in which start by the charecter A and check upto z, (or whatever your requiremtn) if it matches nothing, then add it to last array that you are going to show in untititled objects. i have this for contacts. see if it is understandable by u
int numContacts=[cList count];
//NSMutableArray *nonAlphaArray=[[NSMutableArray alloc] init];
NSMutableArray *arrayCollection[27];
for (int i=0; i<27; i++) {
arrayCollection[i]=[NSMutableArray array];
}
for (int i=0; i<numContacts; i++)
{
Contact *contact= [cList objectAtIndex:i];
unichar alphaSmall='a';
unichar alphaBig='A';
unichar first=0x0000;
if([contact.mContactName length]>0)
first= [contact.mContactName characterAtIndex:0];
for (int j=0; j<26; )
{
if (first==alphaSmall || first==alphaBig)
{
[arrayCollection[j] addObject:contact];
break;
}
alphaSmall++;
alphaBig++;
j++;
if (j==26) {
[arrayCollection[26] addObject:contact];
}
}
}
for (int i=0; i<27; i++)
{
[alphaDictionary setObject:arrayCollection[i] forKey:[NSString stringWithFormat:#"%d",i]];
}