TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
SimpleRemoteObject is a simple Objectie-C library that can create your classes' instance from server-side JSON text. I created this library inspired by NSRails framework. NSRails is awesome framework but it is suitable for Active-Record. Since I had to read data from NON-RAILS server, I made own library.
For example, you can get User class instance with below code.
[User fetchAsync:^(NSArray *allRemote, NSError *error) {
if (error){
//Do error handling
}else{
for (User* user in allRemote){
NSLog(@"my name is:%@",user.name);
}
}
}];
Currently this library is providing just simple usecase. Feel free to add your own usecases and contribute! ;)
Install
SimpleRemoteObject is supporting cocoapods.
Please add below line to your Podfile and run pod install
.
pod 'SimpleRemoteObject'
Make an Objective-C class and have it subclass SRSimpleRemoteObject and set properties.
For example, if the server return below JSON for http://your_server/users.json
:
{
meta: {
limit: 20,
next: null,
offset: 0,
previous: null,
total_count: 3
},
objects: [
{
id: 2,
name: "Daniel",
email: "[email protected]",
age: 25
},
{
id: 2,
name: "Mario",
email: "[email protected]",
age: 30
},
{
id: 3,
name: "Hal",
email: "[email protected]",
age: 32
}
]
}
You can make an object like this.
#import <SimpleRemoteObject/SRSimpleRemoteObject.h>
@interface User : SRSimpleRemoteObject
@property(nonatomic,retain) NSString *name;
@property(nonatomic,retain) NSString *email;
@property(nonatomic) int age;
@end
Implement representUrl and resultKey methods
#import "User.h"
@implementation Tag
+(NSString *)representUrl{
return @"users.json";
}
+(NSString *)resultKey{
return @"objects"; // key of target JSON object
}
@end
Set base url of server API on somewhere.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[SRRemoteConfig defaultConfig].baseurl = SERVER_URL;
// Override point for customization after application launch.
return YES;
}
Retrieve data
[User fetchAsync:^(NSArray *allRemote, NSError *error) {
if (error){
//Do error handling
}else{
for (User* user in allRemote){
NSLog(@"my name is:%@",user.name);
}
}
}];
Sorry, not provided yet.
Test Code may help your understanding.
SimpleRemoteObject is available under the MIT license. See the LICENSE file for more info.
Version 0.0.6
SimpleRemoteObject is written and maintained by Hal Seki. I've learned and inspired by NSRails project, many thanks there!