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

mike1360

macrumors newbie
Original poster
Nov 9, 2013
13
0
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;
    
}
 
Last edited by a moderator:
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.
 
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;
    
}

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.
 
Last edited by a moderator:
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
 
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

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.
 
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.

I really appreciate the help and it makes a bit more of sense now. thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.