title: 斯坦福大學(xué)iOS公開課學(xué)習(xí)筆記(2)-創(chuàng)建類溃槐、方法和翻紙牌小Demo
date: 2017-05-04 14:14:42
tags:
五一的時(shí)候出去玩了一圈贝室,回來終于有時(shí)間整理第二節(jié)課的東西了娇掏。
這一節(jié)課的上半節(jié)課主要講了初始化一些類時(shí)候用到的知識點(diǎn)浙踢,在下半節(jié)課使用Xcode創(chuàng)建了一個翻紙牌的小Demo瞎访。
懶加載
懶加載是什么
懶加載,也稱作延時(shí)加載叹侄,顧名思義懶加載就是在需要到他的前一秒才開始加載休弃,而不是在類初始化的時(shí)候就去加載,這樣做可以減少內(nèi)存的占用圈膏。其實(shí)懶加載就是重寫他的getter方法。
懶加載有什么好處
- 不用將所有的初始化代碼都寫在
viewDidLoad:
中篙骡,加強(qiáng)了代碼的可讀性 - 每個變量的getter方法負(fù)責(zé)自己的實(shí)例化處理稽坤,加強(qiáng)獨(dú)立性。
- 節(jié)約內(nèi)存占用糯俗,只有到真正要使用的時(shí)候才去加載尿褪。
如何實(shí)現(xiàn)
@interface ViewController ()
@property (nonatomic ,strong) NSMutableArray *cards;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSMutableArray *)cards
{
if(_cards == nil)
_cards = [[NSMutableArray alloc] init];
return _cards;
}
數(shù)組的簡單使用
課中主要介紹了一下可變數(shù)組的簡單使用,比如插入得湘、刪除的寫法杖玲。還有使用@[ ]方法快速創(chuàng)建一個數(shù)組。
需要注意的是:
- 在插入元素的時(shí)候一定不能插入空元素淘正,不然會導(dǎo)致程序發(fā)生崩潰摆马。
- 數(shù)組訪問時(shí)要保證數(shù)組下標(biāo)不能越界,不然也會導(dǎo)致程序發(fā)生崩潰鸿吆。
插入的簡單使用
- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
if(atTop)
{
[self.cards insertObject:card atIndex:0];
}
else
{
[self.cards addObject:card];
}
}
刪除的簡單使用
- (Card *)drawRandomCard
{
Card *randomCard = nil;
//這里注意判斷是否下標(biāo)越界
if([self.cards count])
{
unsigned index = arc4random() % [self.cards count];
randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
}
return randomCard;
}
使用@[ ]快速創(chuàng)建一個數(shù)組
- (NSString *)contents
{
//這里使用@[ ]創(chuàng)建一個數(shù)組
NSArray *rankStrings = @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];
return [rankStrings[self.rank] stringByAppendingString:self.suit];
}
+ 類方法
類方法是發(fā)送消息給類囤采,而不是給對象實(shí)例。類方法不可以使用其他實(shí)例變量惩淳。
一般只有兩種情況下會使用類方法
- 創(chuàng)建事物蕉毯,類的初始化時(shí)。
- 工具方法,返回一個常亮的時(shí)候代虾。
+ (NSArray *)ValidSuits
{
return @[@"??",@"??",@"??",@"??"];
}
- (void)setSuit:(NSString *)suit
{
if ([ [PlayingCard ValidSuits] containsObject:suit]) {
_suit = suit;
}
}
init方法
init方法用作初始化時(shí)进肯,他必須與alloc成對出現(xiàn)并且緊跟alloc后。而且只調(diào)用一次棉磨。
instancetype
作為方法的返回值江掩,只能作為返回值,不能像id
那樣作為參數(shù)含蓉。
- (instancetype)init
{
self = [super init];
if(self)
{
for(NSString *suit in [PlayingCard validSuits])
{
for(NSUInteger rank = 1; rank <= [PlayingCard maxRank]; rank++)
{
PlayingCard *card = [[PlayingCard alloc] init];
card.rank = rank;
card.suit = suit;
[self addCard:card];
}
}
}
return self;
}
翻紙牌小Demo
這個Demo主要演示了如何使用storyboard來創(chuàng)建UI并且將它與代碼相關(guān)聯(lián)频敛。整體的使用下來并沒有太多的代碼。完成之后的代碼和storyboard的截圖如下馅扣。
完成之后的樣式圖:
按鈕相關(guān)的代碼
- (IBAction)touchCardButton:(UIButton *)sender
{
if([sender.currentTitle length])
{
UIImage *cardImage = [UIImage imageNamed:@"cardBcak"];
[sender setBackgroundImage:cardImage forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
}
else
{
UIImage *cardImage = [UIImage imageNamed:@"RectanglecardFace"];
[sender setBackgroundImage:cardImage forState:UIControlStateNormal];
[sender setTitle:@"A??" forState:UIControlStateNormal];
}
self.flipCount++;
}
計(jì)數(shù)框相關(guān)的代碼斟赚,這里重寫了filpCount的setter方法來更改計(jì)數(shù)框中的值。
- (void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d",self.flipCount];
}