Incompatible Pointer Type

Discussion in 'iOS Programming' started by mike1360, Oct 22, 2014.

  1. mike1360, Oct 22, 2014
    Last edited by a moderator: Oct 23, 2014

    mike1360 macrumors newbie

    Joined:
    Nov 9, 2013
    #1
    Hello, I'm pretty new to coding. I have taken some lessons and now trying to apply the knowledge into making my very own app. I'm running into a problem now where the line of code below starting with "MainImage *imageView" is getting an error stating:

    "Incompatible pointer type initializing 'MainImage *' with an expression type 'NSArray*' "

    thanks!

    Code:
    -(void)updateGamePlay {
        
        MainImage *imageView = self.mainImages;
        self.mainImageView.image = imageView.mainImage;
        
    }
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    What should the code do, because as is you have the exact problem it's saying: you have an array (possibly of images, but still, an array) and you're trying to use it as an image.
     
  3. TheWatchfulOne, Oct 22, 2014
    Last edited by a moderator: Oct 23, 2014

    TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #3
    Assuming self.mainImages is an NSArray and you are trying to get an image (or some other type of object) out of it, you need to tell it the index of the object you want. Like so:

    Code:
    MainImage *imageView = self.mainImages[n];
    Where "n" is the index of the image you are trying to get.

    It would help to know more about your MainImage class. From the code you posted, it sounds like MainImage is a class that represents not an image, but rather an object that can contain an image.
     
  4. mike1360 thread starter macrumors newbie

    Joined:
    Nov 9, 2013
    #4
    Sorry for the lack of info.

    self.mainImages is an NSArray.

    There are objects inside that array that each have its own assigned image. There is an Image View on my storyboard that the future plan is to have one of those images to be randomly displayed when that method is called upon.

    So, what i was trying to do it set the arrays images to be equal to the Image View on my storyboard.

    I hope this clears some stuff up
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    So then you should get a random number n (maybe using arc4random()), make sure it's an integer between 0 and the highest index of your array (the count method should help with that) and then use:

    Code:
    MainImage *imageView = self.mainImages[n];
    As TheWatchfulOne said, or you could do this:

    Code:
    MainImage *imageView = [self.mainImages objectAtIndex:n];
    Which is the same thing but more verbose.
     
  6. mike1360 thread starter macrumors newbie

    Joined:
    Nov 9, 2013
    #6
    I really appreciate the help and it makes a bit more of sense now. thanks!
     

Share This Page