-
-
Save halsk/4967211 to your computer and use it in GitHub Desktop.
NSRails 的なクラスを自前で作ってみた ref: http://qiita.com/items/dc8d46a1da1e13efefe5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"meta": { | |
"limit": 20, | |
"next": null, | |
"offset": 0, | |
"previous": null, | |
"total_count": 3 | |
}, | |
"objects": [ | |
{ | |
"id": 2, | |
"name": "カフェ", | |
"resource_uri": "/api/tag/%E3%82%AB%E3%83%95%E3%82%A7", | |
"slug": "カフェ" | |
}, | |
{ | |
"id": 1, | |
"name": "ハッカソン", | |
"resource_uri": "/api/tag/%E3%83%8F%E3%83%83%E3%82%AB%E3%82%BD%E3%83%B3", | |
"slug": "ハッカソン" | |
}, | |
{ | |
"id": 3, | |
"name": "破滅", | |
"resource_uri": "/api/tag/%E7%A0%B4%E6%BB%85", | |
"slug": "破滅" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"meta": { | |
"limit": 20, | |
"next": null, | |
"offset": 0, | |
"previous": null, | |
"total_count": 3 | |
}, | |
"objects": [ | |
{ | |
"id": 2, | |
"name": "カフェ", | |
"resource_uri": "/api/tag/%E3%82%AB%E3%83%95%E3%82%A7", | |
"slug": "カフェ" | |
}, | |
{ | |
"id": 1, | |
"name": "ハッカソン", | |
"resource_uri": "/api/tag/%E3%83%8F%E3%83%83%E3%82%AB%E3%82%BD%E3%83%B3", | |
"slug": "ハッカソン" | |
}, | |
{ | |
"id": 3, | |
"name": "破滅", | |
"resource_uri": "/api/tag/%E7%A0%B4%E6%BB%85", | |
"slug": "破滅" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
[MMRemoteConfig defaultConfig].baseurl = @"http://example.net/api/"; | |
; | |
return YES; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MMRemoteClass.h | |
// MoyaMap | |
// | |
// Created by Haruyuki Seki on 2/16/13. | |
// Copyright (c) 2013 Hacker's Cafe. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
// bloc definition | |
typedef void(^MMFetchCompletionBlock)(NSArray *allRemote, NSError *error); | |
@interface MMRemoteClass : NSObject | |
@property (nonatomic, strong) NSNumber* remoteId; // id property of remote class | |
/* | |
// read JSON data from server and call block with NSArray of instanced Classes. | |
*/ | |
+(void)fetchAsync:(MMFetchCompletionBlock)completionBlock; | |
/* | |
// you should override below methods on a subclass | |
*/ | |
+(NSString *)representUrl; | |
+(NSString *)resultKey; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MMRemoteClass.m | |
// MoyaMap | |
// | |
// Created by Haruyuki Seki on 2/16/13. | |
// Copyright (c) 2013 Hacker's Cafe. All rights reserved. | |
// | |
#import <objc/runtime.h> | |
#import "MMRemoteClass.h" | |
#import "MMRemoteConfig.h" | |
#import "AFNetworking.h" | |
#import "MMPropertyUtil.h" | |
@implementation MMRemoteClass | |
/** | |
// read data from specified URI | |
*/ | |
+(void)fetchAsync:(MMFetchCompletionBlock)completionBlock{ | |
// create url | |
NSString *strurl = [NSString stringWithFormat:@"%@%@", [MMRemoteConfig defaultConfig].baseurl, [[self class] performSelector:@selector(representUrl)]]; | |
NSLog(@"call:%@", strurl); | |
NSURL *url = [NSURL URLWithString:strurl]; | |
// call URL | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { | |
NSLog(@"App.net Global Stream: %@", JSON); | |
// set parsed objects to NSArray | |
NSArray *ret = [[self class] parseJSONArray:[JSON valueForKeyPath:[[self class] performSelector:@selector(resultKey)]]]; | |
// call block method | |
completionBlock(ret,nil); | |
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { | |
NSLog(@"App.net Error: %@", [error localizedDescription]); | |
// error handling | |
completionBlock(nil,error); | |
}]; | |
[operation start]; | |
} | |
#pragma mark - | |
#pragma mark internal method | |
/** | |
// parse JSONArray and create class instance | |
*/ | |
+(NSArray *)parseJSONArray:(NSArray *)array{ | |
NSMutableArray *ret = [NSMutableArray arrayWithCapacity:[array count]]; | |
for (NSDictionary *dict in array){ | |
id obj = [[[self class] alloc] init]; | |
[obj performSelector:@selector(setPropertiesUsingRemoteDictionary:) withObject:dict]; | |
[ret addObject:obj]; | |
} | |
return [[NSArray alloc] initWithArray:ret]; | |
} | |
/* | |
// set property from JSON dictionary | |
*/ | |
- (void) setPropertiesUsingRemoteDictionary:(NSDictionary *)dict | |
{ | |
if ([dict objectForKey:@"id"]){ | |
self.remoteId = [dict objectForKey:@"id"]; | |
} | |
NSDictionary *props = [MMPropertyUtil classPropsFor:[self class]]; | |
for (NSString *key in [props allKeys]){ | |
NSLog(@"key:%@", key); | |
if ([dict objectForKey:key]){ | |
[self setValue:[dict objectForKey:key] forKey:key]; | |
} | |
} | |
} | |
#pragma mark - | |
#pragma mark below methods shoul be doverride in a subclass | |
+(NSString *)representUrl{ | |
[self doesNotRecognizeSelector:_cmd]; | |
return nil; | |
} | |
+(NSString *)resultKey{ | |
[self doesNotRecognizeSelector:_cmd]; | |
return nil; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MMRemoteConfig.h | |
// MoyaMap | |
// | |
// Created by Haruyuki Seki on 2/16/13. | |
// Copyright (c) 2013 Hacker's Cafe. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface MMRemoteConfig : NSObject | |
@property(nonatomic,strong) NSString *baseurl; | |
+ (MMRemoteConfig *) defaultConfig; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MMRemoteConfig.m | |
// MoyaMap | |
// | |
// usage: | |
// [MMRemoteConfig defaultConfig].baseurl = @"http://example.net/api/"; | |
// | |
// Created by Haruyuki Seki on 2/16/13. | |
// Copyright (c) 2013 Hacker's Cafe. All rights reserved. | |
// | |
#import "MMRemoteConfig.h" | |
@implementation MMRemoteConfig | |
static MMRemoteConfig *defaultConfig = nil; | |
/* | |
// singleton | |
*/ | |
+ (MMRemoteConfig *) defaultConfig | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^ | |
{ | |
MMRemoteConfig *newConfig = [[MMRemoteConfig alloc] init]; | |
[newConfig useAsDefault]; | |
}); | |
return defaultConfig; | |
} | |
- (void) useAsDefault | |
{ | |
defaultConfig = self; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MMTag.h | |
// MoyaMap | |
// | |
// Created by Haruyuki Seki on 2/16/13. | |
// Copyright (c) 2013 Hacker's Cafe. All rights reserved. | |
// | |
#import "MMRemoteClass.h" | |
@interface MMTag : MMRemoteClass | |
@property(nonatomic,retain) NSString *name; | |
@property(nonatomic,retain) NSString *resource_uri; | |
@property(nonatomic,retain) NSString *slug; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MMTag.m | |
// MoyaMap | |
// | |
// Created by Haruyuki Seki on 2/16/13. | |
// Copyright (c) 2013 Hacker's Cafe. All rights reserved. | |
// | |
#import "MMTag.h" | |
@implementation MMTag | |
/* | |
// baseurl に続くURL | |
*/ | |
+(NSString *)representUrl{ | |
return @"tag"; | |
} | |
/* | |
// JSON 内の結果配列用キー | |
*/ | |
+(NSString *)resultKey{ | |
return @"objects"; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[MMTag fetchAsync:^(NSArray *allRemote, NSError *error) { | |
// allRemote に MMTag の配列が入っているので好きなようにする | |
if (!error){ | |
for (MMTag *tag in allRemote){ | |
NSLog(@"name is %@", tag.name); | |
} | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment