码迷,mamicode.com
首页 > 移动开发 > 详细

IOS7:学习笔记2

时间:2016-01-02 18:28:35      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

一、Deck类

.h文件

 1 #import <Foundation/Foundation.h>
 2 #import "Card.h"
 3 
 4 @interface Deck : NSObject
 5 
 6 //atTop是方法名的一部分
 7 //addCard有两个参数,参数穿插在方法名间
 8 -(void)addCard:(Card *)card atTop:(BOOL)atTop;
 9 
10 //addCard方法的重载,用重载的方式实现可选参数
11 -(void)addCard:(Card *)card;
12 
13 -(Card *)drawRandomCard;
14 
15 
16 @end

.m文件

 1 #import "Deck.h"
 2 
 3 @interface Deck()
 4 @property (strong,nonatomic) NSMutableArray *cards; //of Card
 5 @end
 6 
 7 @implementation Deck
 8 
 9 -(NSMutableArray *)cards
10 {
11     if (!_cards)
12     {
13         _cards=[[NSMutableArray alloc] init];
14     }
15     return _cards;
16 }
17 
18 -(void)addCard:(Card *)card atTop:(BOOL)atTop
19 {
20     if (atTop) {
21         [self.cards insertObject:card atIndex:0];
22     }
23     else
24     {
25         [self.cards addObject:card];
26     }
27 }
28 
29 -(void)addCard:(Card *)card
30 {
31     [self.cards addObject:card];
32 }
33 
34 -(Card *)drawRandomCard
35 {
36     Card *randomCard=nil;
37     
38     if ([self.cards count]) {
39         unsigned index=arc4random()%[self.cards count];
40         randomCard=self.cards[index];
41         [self.cards removeObjectAtIndex:index];
42     }
43     return randomCard;
44 }
45 
46 @end

 

IOS7:学习笔记2

标签:

原文地址:http://www.cnblogs.com/hzz521/p/5095010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!