File tree Expand file tree Collapse file tree
JSONModelDemoTests/UnitTests
JSONModel/JSONModelTransformations Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -222,4 +222,18 @@ -(NSString*)__JSONObjectFromNSDate:(NSDate*)date
222222 return [dateFormatter stringFromDate: date];
223223}
224224
225+ #pragma mark - hidden transform for empty dictionaries
226+ // https://github.com/icanzilb/JSONModel/issues/163
227+ -(NSDictionary *)__NSDictionaryFromNSArray : (NSArray *)array
228+ {
229+ if (array.count ==0 ) return @{};
230+ return (id )array;
231+ }
232+
233+ -(NSMutableDictionary *)__NSMutableDictionaryFromNSArray : (NSArray *)array
234+ {
235+ if (array.count ==0 ) return [[self __NSDictionaryFromNSArray: array] mutableCopy ];
236+ return (id )array;
237+ }
238+
225239@end
Original file line number Diff line number Diff line change @@ -28,6 +28,15 @@ @interface RModel: JSONModel
2828@implementation RModel
2929@end
3030
31+ #pragma mark - empty array/dictionary
32+ @interface DModel : JSONModel
33+ @property (strong , nonatomic ) NSDictionary * dict;
34+ @property (strong , nonatomic ) NSMutableDictionary * mdict;
35+ @end
36+
37+ @implementation DModel
38+ @end
39+
3140#pragma mark - test suite
3241
3342@interface SpecialPropertiesTests : XCTestCase
@@ -64,5 +73,15 @@ - (void)testReadOnly
6473 XCTAssertNotNil (rm, @" model failed to crate" );
6574}
6675
76+ // test auto-converting array to dict
77+ -(void )testEmtpyDictionary
78+ {
79+ NSString * json = @" {\" dict\" :[],\" mdict\" :[]}" ;
80+ DModel* dm = [[DModel alloc ] initWithString: json error: nil ];
81+ XCTAssertNotNil (dm, @" model failed to crate" );
82+ XCTAssertTrue ([dm.dict isKindOfClass: [NSDictionary class ]], @" property did not convert to dictionary" );
83+ XCTAssertTrue ([dm.mdict isKindOfClass: [NSMutableDictionary class ]], @" property did not convert to mutable dictionary" );
84+ }
85+
6786@end
6887
Original file line number Diff line number Diff line change @@ -500,7 +500,7 @@ - (void)layoutSubviews {
500500 totalSize.width = MAX (totalSize.width , indicatorF.size .width );
501501 totalSize.height += indicatorF.size .height ;
502502
503- CGSize labelSize = [label.text sizeWithFont: label.font];
503+ CGSize labelSize = [label.text sizeWithAttributes: @{ NSFontAttributeName : label.font } ];
504504 labelSize.width = MIN (labelSize.width , maxWidth);
505505 totalSize.width = MAX (totalSize.width , labelSize.width );
506506 totalSize.height += labelSize.height ;
Original file line number Diff line number Diff line change @@ -100,6 +100,7 @@ @interface TopModel : JSONModel
100100@property (strong , nonatomic ) JSONAnswer<Optional>* answer;
101101@property (assign , nonatomic , readonly ) int rId;
102102@property (nonatomic , copy ) void (^userLocationCompleted)();
103+ @property (strong , nonatomic ) NSDictionary * dict;
103104@end
104105
105106@implementation TopModel
@@ -113,7 +114,7 @@ @implementation MasterViewController
113114
114115-(void )viewDidAppear : (BOOL )animated
115116{
116- NSString * json = @" {\" id\" :1, \" answer\" : {\" name1\" :\" marin\" }}" ;
117+ NSString * json = @" {\" id\" :1, \" answer\" : {\" name1\" :\" marin\" }, \" dict \" :[] }" ;
117118 TopModel* tm = [[TopModel alloc ] initWithString: json error: nil ];
118119 NSLog (@" tm: %@ " , tm);
119120}
You can’t perform that action at this time.
0 commit comments