Model--MJExtension

10401 ワード

リファレンスドキュメント
MJExtensionは、辞書とモデルを互いに変換する超軽量級フレーム辞書(JSON)-->モデル(Model)-->辞書(JSON)辞書配列(JSON Array)-->モデル配列(Model Array)-->辞書配列(JSON Array)
MJExtension JSOnModel、Mantleの違い
  • MJExtension>JSOnModel>Mantle(変換効率)
  • JSOnModel:すべてのモデルクラスがJSOnModelベースクラスMJExtensionから継承されなければならないことを要求する:特殊なベースクラスを継承する必要はなく、汚染もなく、侵入性もないMantle:すべてのモデルクラスがMTModelベースクラス
  • から継承されなければならないことを要求する
  • 個人感覚ではMJExtensionの許容誤差がもっと高い
  • 1.辞書回転モデル
    typedef enum {
        SexMale,    
        SexFemale} Sex;
    @interface User : NSObject
    @property (copy, nonatomic) NSString *name;
    @property (copy, nonatomic) NSString *icon;
    @property (assign, nonatomic) int age;
    @property (assign, nonatomic) double height;
    @property (strong, nonatomic) NSNumber *money;
    @property (assign, nonatomic) Sex sex;
    @end
    
    NSDictionary *dict = @{
                            @"name" : @"Jack",                 
                            @"icon" : @"lufy.png",               
                            @"age" : @20,               
                            @"height" : @"1.55",               
                            @"money" : @100.9,               
                            @"sex" : @(SexFemale)            
    };
    User *user = [User objectWithKeyValues:dict];
    
    

    2.モデルにネストされたモデル
    @interface Status : NSObject
    @property (copy, nonatomic) NSString *text;
    @property (strong, nonatomic) User *user;
    @property (strong, nonatomic) Status *retweetedStatus;
    @end
    
    NSDictionary *dict = @{               
    @"text" : @"  ,        !", 
    @"user" : @{                   
                 @"name" : @"Jack",                   
                 @"icon" : @"lufy.png"                
               },               
    @"retweetedStatus" : @{                   
                            @"text" : @"       !",                   
                            @"user" : @{                       
                                        @"name" : @"Rose",                       
                                        @"icon" : @"nami.png"                    
                                       }                
                           }            
    };
    
    //      Status  
    Status *status = [Status objectWithKeyValues:dict];
    

    3.モデルに配列がある
    @interface Ad : NSObject
    @property (copy, nonatomic) NSString *image;
    @property (copy, nonatomic) NSString *url;
    @end
    
    @interface StatusResult : NSObject
    /** (    Status  ) */
    @property (strong, nonatomic) NSMutableArray *statuses;
    /** (    Ad  ) */
    @property (strong, nonatomic) NSArray *ads;
    @property (strong, nonatomic) NSNumber *totalNumber;
    @end
    
    @implementation StatusResult
    //   MJExtension  statuses ads      
     + (NSDictionary *)objectClassInArray{    
      return @{         
                    @"statuses" : [Status class],         
                    @"ads" : [Ad class]   
                 };
    }
    + (Class)objectClassInArray:(NSString *)propertyName{    
       if ([propertyName isEqualToString:@"statuses"]) {        
              return [Status class];    
    } else if ([propertyName isEqualToString:@"ads"]) {        
            return [Ad class]; 
       }    
        return nil;
    }
    //          2             ,       Status Ad    
    + (NSDictionary *)objectClassInArray{    
        return @{         
                  @"statuses" : @"Status",         
                  @"ads" : @"Ad"    
                };
    }
    @end
    
    NSDictionary *dict = @{                       
    @"statuses" : @[                           
                    @{                                   
                       @"text" : @"       !",
                       @"user" : @{                                   
                                    @"name" : @"Rose",
                                    @"icon" : @"nami.png"                                                                                  
                                  }                            
                     }, 
    
                     @{                               
                        @"text" : @"      ", 
                        @"user" : @{                                                                   
                                     @"name" : @"Jack",                                       
                                     @"icon" : @"lufy.png"                               
                                   }  
                       } 
    
                    ],                       
     @"ads" :@[                           
               @{                               
                  @"image" : @"ad01.png", 
                  @"url" : @"http://www.ad01.com"                                                          
                }, 
    
               @{                               
                  @"image" : @"ad02.png",                                   
                  @"url" : @"http://www.ad02.com"                           
                }                       
              ],                       
     @"totalNumber" : @"2014"                    
    };
    
             //      StatusResult  
             StatusResult *result = [StatusResult objectWithKeyValues:dict];
    
             //   statuses        
             for (Status *status in result.statuses) {    
                     NSLog(@"text=%@, name=%@", text, name);
              }
             //   ads        
             for (Ad *ad in result.ads) {    
                     NSLog(@"image=%@, url=%@", ad.image, ad.url);
           }
    

    4.Key値が異なる
    @interface Bag : NSObject
    @property (copy, nonatomic) NSString *name;
    @property (assign, nonatomic) double price;@end@interface Student : NSObject
    @property (copy, nonatomic) NSString *ID;
    @property (copy, nonatomic) NSString *desc;
    @property (copy, nonatomic) NSString *nowName;
    @property (copy, nonatomic) NSString *oldName;
    @property (copy, nonatomic) NSString *nameChangedTime;
    @property (strong, nonatomic) Bag *bag;
    @end
    
    @implementation Student
    //   MJExtension                 key
    + (NSDictionary *)replacedKeyFromPropertyName{    
         return @{                
                   @"ID" : @"id",                
                   @"desc" : @"desciption",                
                   @"oldName" : @"name.oldName", 
                   @"nowName" : @"name.newName",  
                   @"nameChangedTime" : @"name.info.nameChangedTime",                                       
                   @"bag" : @"other.bag"            
                  };
    }
    
    @end
    
    NSDictionary *dict = @{                       
                            @"id" : @"20",                       
                            @"desciption" : @"  ",
                            @"name" : @{                                                         
                                         @"newName" : @"lufy", 
                                         @"oldName" : @"kitty",  
                                         @"info" : @{                                                                                                   
                                                      @"nameChangedTime" : @"2013-08"                            
                                                     }                       
                                        },                       
                            @"other" : @{                            
                                          @"bag" : @{                                
                                                      @"name" : @"   ", 
                                                      @"price" : @100.7                                                              
                                                     }                       
                                         }                   
    };
    
    //      Student  
    Student *stu = [Student objectWithKeyValues:dict];
    

    5.ディクショナリ配列をモデル配列に変換
    NSArray *dictArray = @[                       
                            @{                           
                               @"name" : @"Jack",                           
                               @"icon" : @"lufy.png",                        
                             },                       
                            @{                           
                               @"name" : @"Rose",                           
                               @"icon" : @"nami.png",                        
                             }                    
                           ];
    
    //        User    
    NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray];
    //   userArray    User    
    for (User *user in userArray) {   
          NSLog(@"name=%@, icon=%@", user.name, user.icon);
    }
    

    6.モデル辞書
    //     
      User *user = [[User alloc] init];
    user.name = @"Jack";
    user.icon = @"lufy.png";
    
    Status *status = [[Status alloc] init];
    status.user = user;
    status.text = @"       !";
    
    
    //        
    NSDictionary *statusDict = status.keyValues;
    NSLog(@"%@", statusDict);
    /*{ text = "       !";    
        user = {        
                 icon = "lufy.png";        
                 name = Jack;    
               };
      }*/
    
    //        
    Student *stu = [[Student alloc] init];
    stu.ID = @"123";
    stu.oldName = @"rose";
    stu.nowName = @"jack";
    stu.desc = @"handsome";
    stu.nameChangedTime = @"2018-09-08";
    
    Bag *bag = [[Bag alloc] init];
    bag.name = @"   ";
    bag.price = 205;
    stu.bag = bag;
    NSDictionary *stuDict = stu.keyValues;
    

    7.モデル配列辞書配列
    User *user1 = [[User alloc] init];
    user1.name = @"Jack";
    user1.icon = @"lufy.png";
    
    User *user2 = [[User alloc] init];
    user2.name = @"Rose";
    user2.icon = @"nami.png";
    
    NSArray *userArray = @[user1, user2];
    //            
    NSArray *dictArray 
    = [User keyValuesArrayWithObjectArray:userArray];