読者です 読者をやめる 読者になる 読者になる

DRY

Web関連の技術の事、食事/お酒の事、旅行の事など

仮想ザッカーバーグの試験(笑)

Life is beautifulにこんな記事があったので(笑)、ちょっと挑戦してみる事に

HTTPLoader.h


//
// HTTPLoader.h
// HTTPLoader
//
#import

typedef void (^HTTPLoaderComplete)(NSData* data, NSError *error);
@interface HTTPLoader : NSObject

+ (void)loadAsync:(NSURLRequest*)request complete:(HTTPLoaderComplete)loaderComplete;
@end

HTTPLoader.m


//
// HTTPLoader.m
// HTTPLoader
//
#import "HTTPLoader.h"

@implementation HTTPLoader
+ (void)loadAsync:(NSURLRequest*)request complete:(HTTPLoaderComplete)loaderComplete
{
dispatch_queue_t g_queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async (g_queue, ^{
NSLog (@"Execute g_queue");

NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];

loaderComplete (data, error);
});
}
@end

HTTPLoaderTest.h


//
// HTTPLoaderTests.h
// HTTPLoaderTests
//
#import
#import "HTTPLoader.h"

@interface HTTPLoaderTests : SenTestCase

- (void)testloadAsync;
@end

HTTPLoaderTest.m


//
// HTTPLoaderTests.m
// HTTPLoaderTests
//

#import "HTTPLoaderTests.h"

@implementation HTTPLoaderTests
- (void)setUp
{
[super setUp];

// Set-up code here.
}

- (void)tearDown
{
// Tear-down code here.

[super tearDown];
}

- (void)excute:(NSString*)url
{
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[HTTPLoader loadAsync:request
complete:^(NSData* data, NSError *error) {
if (data)
{
NSLog (@"Success: process the data [URL = %@]", url);
}
else
{
NSLog (@"Error: process the error [URL = %@]", url);
}
}];
}

- (void)testloadAsync
{
[self excute:@"http://www.google.co.jp"];
[self excute:@"http://d.hatena.ne.jp/ke-16"];
[self excute:@"http://takoikasaru"];
}
@end

全然45分で終わらなかったのと、関数の引数がblockだった場合の処理の仕方がうる覚え(というか作法がわかってない)だった。。。ので出直しと言う事で。
これがクラスの中身としては一番シンプルな形だと思うのですが。合ってる???