學(xué)習(xí)HM微博項(xiàng)目第11天

步驟:HM微博55-const的使用 -> HM微博56-重構(gòu)

HM微博55-const的使用

Effective Objective-C 2.0的第4條 : 多用類型常量,少用#define預(yù)處理指令,其中關(guān)于全局常量的要點(diǎn)是:在頭文件中使用extern來(lái)聲明全局常量,并在相關(guān)實(shí)現(xiàn)文件中定義其值。這種常量要出現(xiàn)在全局符號(hào)表中,所以其名稱應(yīng)加以區(qū)隔,通常用與之相關(guān)的類名做前綴。 新建一個(gè)HMConst類,如下:
頭文件HMConst.h
實(shí)現(xiàn)文件HMConst.m

由于這個(gè)類要經(jīng)常使用到,將其導(dǎo)入pch文件中。(ps:從xcode6.0和iOS8之后,蘋果官方已經(jīng)不建議使用pch文件)

pch的詳細(xì)代碼如下:

pch文件

HM微博56-重構(gòu)

將前面使用到AFN框架封裝到工具類,只提供get和post請(qǐng)求給外界使用即可。
/* 頭文件HMHttpTool.h  */ 
#import <Foundation/Foundation.h>

@interface HMHttpTool : NSObject

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;

@end


/* 實(shí)現(xiàn)文件HMHttpTool.m  */ 

#import "HMHttpTool.h"
#import "AFNetworking.h"

@implementation HMHttpTool

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    //1.創(chuàng)建請(qǐng)求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    //2.發(fā)送請(qǐng)求
    [mgr GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    //1.創(chuàng)建請(qǐng)求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    //2.發(fā)送請(qǐng)求
    [mgr POST:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

@end

然后,對(duì)使用網(wǎng)絡(luò)請(qǐng)求的類進(jìn)行修改,如HMOAuthViewController、HMHomeViewController,如下:

HMOAuthViewController,post請(qǐng)求的使用實(shí)例
HMHomeViewController,get請(qǐng)求的使用實(shí)例
導(dǎo)入MJRefresh框架,集成下拉刷新控件和上拉刷新控件
集成下拉刷新控件
集成上拉刷新控件
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,291評(píng)論 25 708
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,176評(píng)論 22 665
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 33,170評(píng)論 6 472
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,555評(píng)論 4 61
  • 1 我總以為 要想得到自己想要的生活 我就要努力我就要進(jìn)取 我總以為 要想得到自己想要的健康 我就要運(yùn)動(dòng)我就要鍛煉...
    燕倚東風(fēng)一縷寒閱讀 397評(píng)論 3 6

友情鏈接更多精彩內(nèi)容