1.懶加載
惰性實例化 簡稱懶加載 lazy instantiation 代碼如下:
@property (nonatomic,strong) NSMutableArray *cards;
- (NSMutableArray *)cards
{
if(!_cards) _cards = [[NSMutableArray alloc]init];
//在get方法里注意不要使用self。如果只是返回實例變量,沒有初始化,返回0 nil,
return _cards;
}
2 可變數(shù)組 NSMutableArray
這個類繼承了NSArray;是可變的,可以在這個數(shù)據(jù)結(jié)構(gòu)上插入、移除元素操作。NSArray數(shù)組,在初始化后,就固定了, 不能做動態(tài)的增刪元素。
@interface NSMutableArray<ObjectType> : NSArray<ObjectType>
//添加元素 到數(shù)組末尾
- (void)addObject:(ObjectType)anObject;
//插入元素到指定位置
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;
//移除最后一個元素
- (void)removeLastObject;
//移除指定位置元素
- (void)removeObjectAtIndex:(NSUInteger)index;
//把指定位置的對象替換為其它對象
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject;
//初始化方法
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
//還有一個category的分類,也對可變數(shù)組有一些操作,都很好理解,大家可以看下
@interface NSMutableArray<ObjectType> (NSExtendedMutableArray)
3 使用stroryboard構(gòu)建UI界面
在sb故事板中構(gòu)建UI,很快速,只需要拖動組件到界面中去,點擊相應(yīng)組件會展示出組件所擁有的屬性,*** 所見即所得 *** 的效果?!?/p>
在sb中給控件添加事件方法, 只需拖線即可,
返回值為IBAction,實際上是typedef void ,之所以定義為IBAction 只是為了分辨那個方法是目標(biāo)動作
- (IBAction)touchCardButton:(UIButton *)sender
{
if ([sender.currentTitle length])
{
[sender setBackgroundImage:[UIImage imageNamed:@"cardBack"]
forState:UIControlStateNormal];
[sender setTitle:@""
forState:UIControlStateNormal];
}else
{
Card *card = [self.deck drawRandomCard];
[sender setBackgroundColor:[UIColor whiteColor]];
[sender setBackgroundImage:nil forState:UIControlStateNormal];
[sender setTitle:card.contents forState:UIControlStateNormal];
}
//同時調(diào)用了setter和getter
self.flipsCount ++;
}
還可以把控件通過IBOutlet與外界關(guān)聯(lián)
通過sb與外界關(guān)聯(lián)怎么會 為什么會是weak呢? 因為標(biāo)簽已經(jīng)由視圖所持有,所以這用weak。
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
4 查看相關(guān)文檔的方法。
所到最后,這里我覺得應(yīng)該是 __ 重點 __ , 開發(fā)者最應(yīng)該習(xí)慣文檔,會查找文檔,會看文檔。 這才是我們應(yīng)該最主要學(xué)習(xí)的, 在Xcode中,按住Command鍵 ,點擊關(guān)鍵字,可以進(jìn)入相應(yīng)的頭文件, 按住Option鍵,會在本頁面彈出關(guān)鍵字簡短介紹, Command + shitf + 0 進(jìn)入文檔搜索頁面。