學習Coding-iOS開源項目日志(一)


前言:作為初級程序員,想要提高自己的水平,其中一個有效的學習方法就是學習別人好的項目。本篇開始會陸續更新本人對github上開源的一個很不錯的項目的一點點學習積累。也就是,探究着別人寫的源碼,我學到了新的什么東西?本人愚拙,而且碼齡不多,也就三年左右,水平不高,如有挫解,還望指正。本人樂愛學習,樂於分享,廣結良緣,願意交流。當然,高手可以飄過。

Coding-iOS項目網址:https://github.com/Coding/Coding-iOS 讀者感興趣的可以自己去下載,當然項目很多第三方框架是沒有直接集成進來的,讀者自行通過該項目的提示處理。

 

另外還有官網介紹:https://coding.net/u/coding/p/Coding-iOS/git#rd

 

 

內容概要:

1、關於MobClick,友盟統計的使用

2、關於Google Analytics

3、關於Debug

4、關於RDVTabBarController

5、關於GCC語法

6、關於TMCache的使用

7、關於TTTAttributedLabel的使用

 

正文:

2016年3月21日

文件:BaseViewController.m

1、下面代碼添加友盟統計,設置狀態欄,代碼設置豎屏。

 1 - (void)viewWillAppear:(BOOL)animated
 2 {
 3     [super viewWillAppear:animated];
 4     // hy:友盟統計,https://github.com/liyoro/UMAnalytics
 5     // hy:標哥的博客:http://www.henishuo.com/ios-umeng-push/
 6     [MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
 7     // hy:設置狀態欄
 8     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
 9 
10     // hy:如果不是豎屏、不支持豎屏或是橫屏
11     if (self.interfaceOrientation != UIInterfaceOrientationPortrait
12         && !([self supportedInterfaceOrientations] & UIInterfaceOrientationMaskLandscapeLeft)) {
13         // hy:設置成豎屏
14         [self forceChangeToOrientation:UIInterfaceOrientationPortrait];
15     }
16 }
17 ......
18 - (void)forceChangeToOrientation:(UIInterfaceOrientation)interfaceOrientation{
19     [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:interfaceOrientation] forKey:@"orientation"];
20 }

文件:MobClick.h 是友盟統計的SDK接口文件。具體使用以后補充,先知道這個類是這么回事。

2、下面代碼中用了Google Analytics。

關於集成這個Google Analytics的SDK學習的網站:https://www.raywenderlich.com/53459/google-analytics-ios (外國網站)

對應的國內翻譯網站:http://www.cocoachina.com/industry/20140108/7674.html

 1 - (void)viewDidLoad{
 2     [super viewDidLoad];
 3     self.view.backgroundColor = kColorTableBG;
 4     // hy:這里又代碼設置豎屏
 5     if (self.interfaceOrientation != UIInterfaceOrientationPortrait
 6         && !([self supportedInterfaceOrientations] & UIInterfaceOrientationMaskLandscapeLeft)) {
 7         [self forceChangeToOrientation:UIInterfaceOrientationPortrait];
 8     }
 9     // hy:添加了google analytics,Google提供的免費的使用者分析服務
10     // GA
11     id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
12     [tracker set:kGAIScreenName value:[NSString stringWithUTF8String:object_getClassName(self)]];
13     [tracker send:[[GAIDictionaryBuilder createScreenView] build]];
14 }

 3、下面代碼用了宏定義Debug打印模式

- (void)tabBarItemClicked{
    DebugLog(@"\ntabBarItemClicked : %@", NSStringFromClass([self class]));
}

然后我command+click跳轉到下面代碼:

1 #define DebugLog(s, ...) NSLog(@"%s(%d): %@", __FUNCTION__, __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__])

然后我就自己創建新的Simple Project使用了一下:

 4、文件夾:RDVTabBarController

因為在項目源碼中,RootTabViewController : RDVTabBarController<RDVTabBarControllerDelegate>,所以進一步探索RDVTabBarController,發現這個是第三方框架

而且github上點贊量蠻高的,網址是:https://github.com/robbdimitrov/RDVTabBarController 。記錄以后學習學習該源碼做了什么?

3月24日:

 5、在CodingBannersView.m文件中可以發現一枚"GCC語法":

6、關於TMCache的使用:

在Coding-iOS這個項目中,通過pod集成了TMCache這個框架,於是我就對這個框架進行了了解:

TMCache的github地址:https://github.com/tumblr/TMCache

TMCache 是 Tumblr 公司開發的一個快速,無死鎖的並行對象緩存,支持 iOS 和 OS X 系統。

示例代碼:

UIImage *img = [[UIImage alloc] initWithData:data scale:[[UIScreen mainScreen] scale]];
[[PINCache sharedCache] setObject:img forKey:@"image" block:nil]; // returns immediately

[[PINCache sharedCache] objectForKey:@"image"
                              block:^(PINCache *cache, NSString *key, id object) {
                                  UIImage *image = (UIImage *)object;
                                  NSLog(@"image scale: %f", image.scale);
                              }];

不過現在已經停止更新了。

然后再來看Coding-iOS這個項目中的一個TMCacheExtend.h和TMCacheExtend.m文件。

1 #import <Foundation/Foundation.h>
2 #import "TMCache.h"
3 
4 @interface TMCache (Extension)
5 
6 + (instancetype)TemporaryCache;
7 + (instancetype)PermanentCache;
8 
9 @end
 1 #import "TMCacheExtend.h"
 2 
 3 #define kTemporaryCache @"com.dv.cache.temporary"
 4 #define kPermanentCache @"com.dv.cache.permanentCache"
 5 
 6 @implementation TMCache (Extension)
 7 
 8 + (instancetype)TemporaryCache{
 9     return [[TMCache sharedCache] initWithName:kTemporaryCache];
10 }
11 + (instancetype)PermanentCache {
12     return [[TMCache sharedCache] initWithName:kPermanentCache];
13 }
14 
15 @end

看的出這個拓展(但不是類別,僅僅是普通類,使用了便利構造器的用法),便利出了兩個方法:temporary(臨時的)、permanent Cache(永久的緩存)

然后在CSSearchModel.m文件中,只用了臨時緩存的方法TemporaryCache

7、關於TTTAttributedLabel的使用

這個是點贊超過5K的第三方框架,github網址是:https://github.com/TTTAttributedLabel/TTTAttributedLabel ,簡略的中文博客介紹可以看看:http://each.dog/2015/01/14/read-tttattributedlabel.html ,然后來看看Coding源碼中UITTTAttributedLabel.h是對TTTAttributedLabel的一個繼承拓展,然后多出被使用,其中CSSearchCell.h中就被使用,導入和遵循了協議,在CSSearchCell.m文件中第32行聲明了屬性,然后創建了這個UITTTAttributedLabel對象:

 

如果讀者意猶未盡,可以繼續閱讀本人學習Coding-iOS第二篇《學習Coding-iOS開源項目日志(二)》。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM