File tree Expand file tree Collapse file tree 2 files changed +19
-18
lines changed Expand file tree Collapse file tree 2 files changed +19
-18
lines changed Original file line number Diff line number Diff line change @@ -114,13 +114,21 @@ -(id)init
114
114
115
115
-(instancetype )initWithData : (NSData *)data error : (NSError *__autoreleasing *)err
116
116
{
117
- // turn nsdata to an nsstring
118
- NSString * string = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
119
- if (!string) return nil ;
120
-
121
- // create an instance
117
+ if (!data) return nil ;
118
+
119
+ // read the json
122
120
JSONModelError* initError = nil ;
123
- id objModel = [self initWithString: string usingEncoding: NSUTF8StringEncoding error: &initError];
121
+ id obj = [NSJSONSerialization JSONObjectWithData: data
122
+ options: kNilOptions
123
+ error: &initError];
124
+
125
+ if (initError) {
126
+ if (err) *err = [JSONModelError errorBadJSON ];
127
+ return nil ;
128
+ }
129
+
130
+ // init with dictionary
131
+ id objModel = [self initWithDictionary: obj error: &initError];
124
132
if (initError && err) *err = initError;
125
133
return objModel;
126
134
}
@@ -141,21 +149,11 @@ -(id)initWithString:(NSString *)string usingEncoding:(NSStringEncoding)encoding
141
149
return nil ;
142
150
}
143
151
144
- // read the json
145
152
JSONModelError* initError = nil ;
146
- id obj = [NSJSONSerialization JSONObjectWithData: [string dataUsingEncoding: encoding]
147
- options: kNilOptions
148
- error: &initError];
149
-
150
- if (initError) {
151
- if (err) *err = [JSONModelError errorBadJSON ];
152
- return nil ;
153
- }
154
-
155
- // init with dictionary
156
- id objModel = [self initWithDictionary: obj error: &initError];
153
+ id objModel = [self initWithData: [string dataUsingEncoding: encoding] error: &initError];
157
154
if (initError && err) *err = initError;
158
155
return objModel;
156
+
159
157
}
160
158
161
159
-(id )initWithDictionary : (NSDictionary *)dict error : (NSError **)err
Original file line number Diff line number Diff line change @@ -122,6 +122,9 @@ -(void)viewDidAppear:(BOOL)animated
122
122
TopModel* tm = [[TopModel alloc ] initWithString: json error: nil ];
123
123
NSLog (@" tm: %@ " , tm.toDictionary );
124
124
NSLog (@" to string: %@ " , tm.toJSONString );
125
+ tm = [[TopModel alloc ] initWithData: [json dataUsingEncoding: NSUTF8StringEncoding] error: nil ];
126
+ NSLog (@" tm - WithData : %@ " , tm.toDictionary );
127
+ NSLog (@" to string - WithData : %@ " , tm.toJSONString );
125
128
}
126
129
127
130
-(IBAction )actionLoadCall : (id )sender
You can’t perform that action at this time.
0 commit comments