forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterViewController.m
More file actions
205 lines (158 loc) · 5.5 KB
/
MasterViewController.m
File metadata and controls
205 lines (158 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// MasterViewController.m
// JSONModelDemo
//
// Created by Marin Todorov on 02/12/2012.
// Copyright (c) 2012 Underplot ltd. All rights reserved.
//
#import "MasterViewController.h"
#import "KivaViewController.h"
#import "GitHubViewController.h"
#import "YouTubeViewController.h"
#import "StorageViewController.h"
#import "KivaViewControllerNetworking.h"
#import "JSONModel+networking.h"
#import "VideoModel.h"
#import <objc/runtime.h>
#import <objc/message.h>
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end
/*
#define OPT(type, opt, name) type name; @property id opt name##Property__;
#define OptionalProperties(...) @property BOOL zz_____OptionalPropertiesBegin; __VA_ARGS__ @property BOOL zz_____OptionalPropertiesEnd;
@interface Ignore_Model : JSONModel
//OptionalProperties
//(
@property (strong, nonatomic) NSNumber<Optional>* oName;
//)
@property (assign, nonatomic) OPT(BOOL, <Optional>, name);
@property (strong, nonatomic) NSNumber<Ignore, Optional, Index>* iName;
@property (strong, nonatomic) NSDate* mydate;
@end
@implementation Ignore_Model
//@dynamic nameProperty;
+(BOOL)propertyIsOptional:(NSString*)propertyName
{
return ([propertyName isEqualToString:@"name"]);
}
@end
@interface TopModel : JSONModel
@property (strong, nonatomic) Ignore_Model* im;
@end
@implementation TopModel
@end
*/
#define modelOptional
@protocol JSONAnswer
@end
//
// JSON Connector
//
@protocol JSONAnswerJSON
@required
@property (nonatomic) NSString* name;
@optional
@property (nonatomic) NSString* age;
-(void)importAgeFromJSON:(id)value;
-(id)exportAgeToJSON;
-(void)importAgeFromCoreData:(id)value;
-(id)exportAgeToCoreData;
@end
@interface JSONAnswer : JSONModel
@property (nonatomic, strong) NSString* name;
@end
@implementation JSONAnswer
@end
@interface TopModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) JSONAnswer<Optional>* answer;
@property (assign, nonatomic, readonly) int rId;
@property (nonatomic, copy) void(^userLocationCompleted)();
@property (strong, nonatomic) NSDictionary* dict;
@property (strong, nonatomic) NSString* description;
@end
@implementation TopModel
+(BOOL)propertyIsIgnored:(NSString *)propertyName
{
return NO;
}
-(NSString*)getText
{
return @"1123";
}
@end
@implementation MasterViewController
-(void)viewDidAppear:(BOOL)animated
{
NSString* json = @"{\"id\":1, \"answer\": {\"name1\":\"marin\"}, \"dict\":[], \"description\":\"Marin\"}";
TopModel* tm = [[TopModel alloc] initWithString:json error:nil];
NSLog(@"tm: %@", tm.toDictionary);
NSLog(@"to string: %@", tm.toJSONString);
tm = [[TopModel alloc] initWithData:[json dataUsingEncoding:NSUTF8StringEncoding] error:nil];
NSLog(@"tm - WithData : %@", tm.toDictionary);
NSLog(@"to string - WithData : %@", tm.toJSONString);
}
-(IBAction)actionLoadCall:(id)sender
{
[JSONHTTPClient getJSONFromURLWithString:@"http://localhost/testapi/test.php"
completion:^(NSDictionary *json, JSONModelError *err) {
NSLog(@"GOT: %@", [json allKeys]);
}];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"Demos";
_objects = [NSMutableArray arrayWithArray:@[@"Kiva.org demo", @"GitHub demo", @"Youtube demo", @"Used for storage"]];
}
return self;
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:{
KivaViewController* kiva = [[KivaViewController alloc] initWithNibName:@"KivaViewController" bundle:nil];
[self.navigationController pushViewController:kiva animated:YES];
}break;
case 1:{
GitHubViewController* gh = [[GitHubViewController alloc] initWithNibName:@"GitHubViewController" bundle:nil];
[self.navigationController pushViewController:gh animated:YES];
}break;
case 2:{
YouTubeViewController* yt = [[YouTubeViewController alloc] initWithNibName:@"YouTubeViewController" bundle:nil];
[self.navigationController pushViewController:yt animated:YES];
}break;
case 3:{
StorageViewController* sc = [[StorageViewController alloc] initWithNibName:@"StorageViewController" bundle:nil];
[self.navigationController pushViewController:sc animated:YES];
}break;
default:
break;
}
}
@end