iOS 피닉스 FM 모방
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;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.