adding external input to an array?

Discussion in 'iOS Programming' started by HannKett, Apr 12, 2012.

  1. HannKett, Apr 12, 2012
    Last edited: Apr 12, 2012

    HannKett macrumors member

    Feb 25, 2012
    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:

    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;
  2. itickings macrumors 6502a


    Apr 14, 2007
    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?
  3. HannKett thread starter macrumors member

    Feb 25, 2012
    I just figured it out... trying to sleep it all came to me :)

    Re-initializing it with original values :D (doh!)

Share This Page