We will now discuss the implementationdetails of the method that calculates your currency exchange.
Getting currency information from Yahoo,is as simple as making a GET request.
I wrote a macro to format this URL for a given currency pair.
#define YAHOO_URL(__C1__, __C2__) [NSString stringWithFormat:@"d/quotes.csv?e=.csv&f=sl1d1t1&s=%@%@=X", __C1__, __C2__]
按如下顺序编写 engine类方法:
根据参数准备 URL
创建一个 MKNetworkOperation 对象
设置方法参数
设置 operation 的 completion 块和 error 块(在 completation 块中处理 response 并转换为模型)
可选地,添加一个 progress 块(或者在 view controller 中做这个)
如果 operation 是下载,设置下载流(通常是文件)。这步也是可选的
当 operation 完成,处理结果并调用方法块,并将数据返回给调用者。
示例代码如下:
复制代码MKNetworkOperation *op = [selfoperationWithPath:YAHOO_URL(sourceCurrency, targetCurrency)
params:nil
httpMethod:@"GET"];
[op onCompletion:^(MKNetworkOperation*completedOperation)
{
DLog(@"%@", [completedOperation responseString]);
//do your processing here
completionBlock(5.0f);
}onError:^(NSError* error) {
errorBlock(error);
}];
[self enqueueOperation:op];










