Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HannKett

macrumors member
Original poster
Feb 25, 2012
70
0
Europe
Hello good folks,

once again I need your help as I have stumbled across a seemingly easy issue :) I can't wrap my head around at this hour (1am).

I have a NSMutableArray with 3 objects in it. I take an text input and check if it is in the array. If it is in the array I say good work and exit. If it is not in the array I ask if you would like to add it, and then try to add it with insertobjectatindex:0. but when I re-run the entire loop it is not found in the array.

I also tested this in an CLI app and get the same result from keyboard entry.

I do not re-start the app, I have set up an for loop to keep checking in an infinite loop until i solve this issue.

any advice would be greatly appreciated.


for clarification this is the code:


Code:
int a = 0;
        while (a == 0) {
            
            id RandomObject;
            char namnet[10] = {0}; NSLog(@"\nPlease Enter Username\n"); scanf("%s", namnet);
            RandomObject = [NSString stringWithUTF8String:namnet]; 
            NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"Steve",@"Ben",@"John", nil];
            //NSLog(@"%@ and %@",myString,[myArray objectAtIndex:1]);
            if ([myArray containsObject:RandomObject]) {
                NSLog(@"User is known!!");
            }else {
                id name;
                name = RandomObject;
                NSLog(@"unknown user!");
                NSLog(@"\n Please add user!\n");
                [myArray insertObject:name atIndex:0];
                NSLog(@"%@",[myArray objectAtIndex:0]);
            }  
        }  
        }
           return 0;
}
 
Last edited:
Just step through the program for two loops line by line.
Where are you declaring and creating your array and where are you using it?
 
Just step through the program for two loops line by line.
Where are you declaring and creating your array and where are you using it?

I just figured it out... trying to sleep it all came to me :)

Re-initializing it with original values :D (doh!)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.