tags:

views:

24

answers:

1

Alright, so I am working on a simple card game for Mac OS X. The way I have it set up is with a Card class that holds its own suit and rank, and methods that returns these as NSStrings. What I currently have is a method that pulls random cards out of an array that is the "deck" and puts them into another array that is the player's "hand". I am using NSImages to display the cards onscreen.

What I want to do is call the method in the card that will tell me its suit and rank. Then, I have the image of that card stored elsewhere. Those images are named like 3Diamond.png or JackClub.png, because I was thinking that I could set the image by doing something like this. So basically, I have the rank and the suit in strings, and from that I need to change which UIImage I pick. I realize I could do this with a switch or if else statement but that would be really inefficient.

+1  A: 

Look at the NSString class reference. Specifically, the +stringWithFormat: method. Then use UIImages +imageNamed: method to load your image.

jer
So, you mean like `UIImage *example = [UIImage imageNamed [NSString stringWithFormat @"%@%@", [ExampleCard rank], [ExampleCard suit]];` And then later `CC1.image = example;`
Regan
Actually I am working with an NSImage (I normally do iPhone programming but I am trying this out on the mac so I confused the two) so is there an NSImage equivalent to imageNamed?
Regan
Sorry I gapped not seeing the iphone tag so I guessed. I will direct you to the NSImage class reference for your answer.
jer
No, it's fine, I actually switched back and forth between the two in my answer, but I have solved the problem now, as you made me remember how NS (and UI) Image works so I was able to do what I described in my comment with all the code in it. Thanks.
Regan