iOS 피닉스 FM 모방

2817 단어
코드 주소:https://github.com/tom555cat/FenghuangFM.git
fenghuangFM_01.gif
FM iOS 클라이언트를 모방하는 것은 3가지 목적입니다. 1>네트워크 요청 이해2>reactiveCocoa 이해3>MVVM 이해.

네트워크 요청


봉황FM의 http 요청 반환 데이터는 JSON 형식으로 Chalse로 http 요청을 쉽게 캡처할 수 있으며 구체적인 요청 정보는 FenghuangFM/HTTPRequest 아래에 있습니다.http 요청은 JSON 형식의 데이터로 되돌아오기를 요청합니다. JSON이 MJextension 라이브러리에 사용되었음을 분석합니다. JSON에 list가 있을 때 다음과 같은 방식으로 JSON의 키가 'audiolist'인 list의 모든 요소가' 오디오 '형식임을 알립니다.
[ActivityModel mj_setupObjectClassInArray:^NSDictionary *{
    return @{
        @"audiolist":@"Audio"
    };
}];

또한 되돌아오는 JSON의 키는'new'로 시작할 수 있습니다. 모델을 정의할 때 구성원 변수가 new로 시작하면 오류가 발생할 수 있습니다. 이 때 JSON의 키를 모델의 구성원 변수 이름으로 변환해야 합니다.
[LeaderBoardData mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
    return @{
        @"newsList":@"newList"
    };
}];

reactiveCocoa


reactiveCocoa는 이곳의 응용 프로그램과 인터넷 요청을 결합하여 홈페이지를 불러올 때 두 개의 http 요청을 보내야 합니다. 이 두 요청이 모두 결과를 되돌려받은 후에 다음 단계를 계속하면 reactiveCocoa는 이 동작을 간단하게 완성할 수 있습니다.
MainFeatureViewModel.m
- (void)refreshDataSource
{
    @weakify(self);
    RACSignal *signalFocus = [RACSignal createSignal:^RACDisposable *(id subscriber) {
    @strongify(self);
    [self requestFocusList:^{
        [subscriber sendNext:nil];
    }];
        return nil;
    }];

    RACSignal *signalRest = [RACSignal createSignal:^RACDisposable *(id subscriber) {
    @strongify(self);
    [self requestRest:^{
        [subscriber sendNext:nil];
    }];
    return nil;
    }];

    [[RACSignal combineLatest:@[signalFocus,signalRest]] subscribeNext:^(id x) {
    @strongify(self);
    [(RACSubject *)self.updateContentSignal sendNext:nil];
    }];
}

MVVM


MVVM은 MVC 프레임워크의 복잡하고 방대한 ViewController를 해방시켰다.원래의 ViewController에는'Views+업무'가 포함되어 있었는데, 현재 MVVM은 ViewModel을 사용하여 업무라는 부분을 옮겨 놓았고, 순식간에 ViewController의 체량을 떨어뜨렸다.홈 페이지에서 사용하는 ViewModel 보기:
MainFeatureViewModel.h
@interface MainFeatureViewModel : NSObject

@property (nonatomic, readonly) RACSignal                   *updateContentSignal;

@property (nonatomic, strong)   MainFeatureModel            *featureModel;

@property (nonatomic, strong)   RecommendModel              *recommendModel;

- (void)refreshDataSource;

- (NSInteger)numberOfSections;
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
- (CGFloat)heightForRowAtIndex:(NSIndexPath *)indexPath;

@end


데이터 모델인 Main Feature Model과 Recommend Model 관련 작업은 VC에서 벗어나 이곳으로 옮겨졌고 Main Feature Controller에서view Model 속성을 통해 데이터를 얻을 수 있습니다.
@property (nonatomic, strong)   MainFeatureViewModel    *viewModel;

좋은 웹페이지 즐겨찾기