Objects in Array Won't respond

Discussion in 'iOS Programming' started by xcodeNewbie, Jul 19, 2011.

  1. macrumors member

    Joined:
    Jul 1, 2011
    #1
    I have an NSMutableArray, called myArray. No matter what I do, the objects in it won't respond to any command. Take a look at this code:
    Code:
    UIImageView *myView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.jpg"]];
    myView.frame = CGRectMake(0,0,100,100);
    [self.view addSubview:myView];
    [myView release];
    The above code works perfectly, a UIImageVIew is created in the top left corner of the screen. However, look at this code:
    Code:
    UIImageView *myView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.jpg"]];
    myView.frame = CGRectMake(0,0,100,100);
    [myArray replaceObjectAtIndex:0 withObject:myView];
    [self.view addSubview:[myArray objectAtIndex:0]];
    [myView release];
    With this code, no ImageVIew appears. I've been having trouble for so long with Arrays, it's like when an object is in an array it becomes invisible and unusable.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Time for some basic debugging. What is the value of myArray when you call replaceObjectAtIndex:withObject? Is it non-nil? Has it been initialized? Etc.

    P.S. How and where is myArray defined?
     
  3. thread starter macrumors member

    Joined:
    Jul 1, 2011
    #3
    myArray

    It was initialized with an object count of 1.
     
  4. macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #4
    Huh? Do you mean you created it with a capacity of 1? I don't believe that adds ANY objects in the array, rather it sets some internal allocation size for possible addition of objects.

    Add the following line of code on either side of your "replaceObjectAtIndex:withObject: method:
    Code:
    NSLog(@"myArray has %d objects in it.",[myArray count]);
    Tell us what that prints.
     

Share This Page