1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Objective - C Beginner question

Discussion in 'Mac Programming' started by onqun, Apr 19, 2012.

  1. macrumors member

    Hello, I am trying to make 52 card deck with couple of players, in objective c. I am a freshman. after the interface section I confused, and could not move on.

    #import <Foundation/Foundation.h>

    @interface cards : NSObject

    @property int* spade;


    @interface spade : cards

    @property int spadeCards;


    @interface heart : cards

    @property int heartCards;


    @interface diamond : cards

    @property int diamondCards;


    @interface club : cards

    @property int clubCards;


    @interface players : NSObject

    -(void) player;
    -(void) print;


    @interface dealer : NSObject

    -(void) deal: (int) d;


    ** i wanted to create 4 objects under the object of cards . My aim for the implementation part is to create 13 cards for each by giving them numbers and for the face cards i want to point them to string for print function.

    My problem is, as i said earlier, i am a freshman; i do not know how to put numbers in an array and use them as objects. :/

    @implementation cards

    @synthesize spade;


    thank you for help !
  2. macrumors 68030

    This creates a spade card and sets it's int value to 1, which I assume should be the Ace:
    spade * aceOfSpaces = [[spade alloc] init];
    [aceOfSpades setSpadeCards:1];
    Note that, since you define spadeCards as a property, the setter method setSpadeCards is automatically created by the compiler.

    You should learn some basic C and Objective C.
  3. Vsching, Apr 19, 2012
    Last edited by a moderator: Apr 20, 2012

    macrumors newbie

    A sample answer

    So basically , you already create 4 card types.

    At your main file or view controller class, what you need is just import all card types. just something like this. This is just a simple way of doing it. I am sure there can be much better way of doing once you learn more bout programming :)
          mutableArray = [[NSMutableArray alloc]initWithCapacity:52];
            for (int i = 0; i< 13< i++) {
          Spade *spade= [Spade alloc]init];
          spade.spadeCards = i;
         [mutableArray addObject: spade];
    //repeat for other types, hearts,diamonds..
    And i suggest you have a look on objective c for dummies, it is very good book for learning the basics.
  4. macrumors member

    Thank you for replies. I used have knowledge about basic c, unfortunately I have forgotten. I have two books one of them is programming in Objective-C by developers library, 4e and objective c programming: the big nerd ranch guide by Aaron Hillegass
  5. macrumors 68000

    Have you though about a deck object that contains 52 cards, and each card has a value and suit?
  6. macrumors member

    Do you mean something like this for interface part?

    @interface deck : NSObject
    @property int spade, club, heart, diamond;
  7. macrumors 68000

    You could have one property 'suit'

    And it would just be an enum like
    enum suit
      spade = 1,
  8. macrumors member

    does enum creates a property ? what is the difference between @property and enum?
  9. macrumors 6502a


    You should Google a bit to find this stuff out for yourself. The short answer though is that an enum is treated like an integer, so it works the same way as an integer property. I agree this is the perfect situation for an enum (when there is a discrete list of items to choose from).
  10. onqun, Apr 21, 2012
    Last edited: Apr 21, 2012

    macrumors member

    Hey, thank you for your reply. Of course I do google everything rather than definitions i need more like similar examples. Even though, I do have a experience with coding. However, i can not apply what i think :/
    Since, I do not know objective c deeply, I look at sources and too many sources mixes everything.


    #import <Foundation/Foundation.h>
    @interface cards : NSObject
    @property int spade, diamond, club, heart;
    + (int)cardsOfSpadeWithCapacity:(NSUInteger)numItems;
    + (int)cardsOfDiamondWithCapacity:(NSUInteger)numItems;
    + (int)cardsOfClubWithCapacity:(NSUInteger)numItems;
    + (int)cardsOfHeartWithCapacity:(NSUInteger)numItems;
    @implementation cards
    @synthesize spade, diamond, club, heart;
    I changed the code with these lines. I am hoping to create arrays for every suit and assign them their to appropriate suit as an object. I hope this is right way to do it. I do not know how to use enum therefor, i do not want to use it.

Share This Page