RosettaStoneKit 1.5.2

RosettaStoneKit 1.5.2

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最新发布2017年8月

Chris Stephan 维护。



  • Chris

RosettaStoneKit 是一个神奇的对象映射框架。它可以将在字典和数组中转换为任何类的实例。它还可以将您的自定义对象转换为字典和数组。

动机

有其他一些框架提供了相同的功能,但它们都做出了限制它们用途的设计决策。此框架不需要您从属于一些特殊的类,该类为您添加了所有魔法功能。它不需要在 NSObject 上添加类别以使每个对象都带有额外的方法。最后,目标是做一件事情,并且做好。此框架不包含网络代码或其他任何功能。

功能

  • 小型公共 API 接口
  • 可与其他任何 NSObject 子类一起使用
  • 通过自定义 PropertyTranslator 自定义
  • 将对象转换为 NSDictionary
  • NSDictionary 转换为对象
  • NSArrayNSDictionary 上运行
  • 文档齐全
  • 测试良好
  • 支持 Cocoapods/Carthage 集成

查看代码

// User.h

@interface User

@property (nonatomic, copy)   NSString *firstName;
@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSDate *birthday;
@property (nonatomic, strong) NSString *lastName;

@end
// Elsewhere

#import <RosettaStoneKit/RosettaStoneKit.h>

NSDictionary *userDictionary = @{
  @"firstName": @"Joe",
  @"userId": 42,
  @"birthday": @"1970-01-01T00:00:00.000Z",
  @"lastName": @"User"
};

RosettaStone *stone = [RosettaStone sharedInstance];

User *joeUser = [stone translate:userDictionary toClass:[User class]];

当事情不尽如人意时

// Team.h

@interface Team

@property (nonatomic, copy)   NSString *name;
@property (nonatomic, strong) NSNumber *teamNumber;
@property (nonatomic, strong) NSArray *games;

@end
// Elsewhere

#import <RosettaStoneKit/RosettaStoneKit.h>

NSDictionary *teamDictionary = @{
  @"name": @"Piggers",
  @"number": 42,
  @"games": @[
    @{@"name": @"Table Hocky championships"},
    @{@"name": @"Ping Pong championships"}
  ]
};

RosettaStone *stone = [RosettaStone sharedInstance];

PropertyTranslator *numberTranslator = [propertyTranslatorForClass:[Team class]
                                                           fromKey:@"number"
                                                        toProperty:@"teamNumber"];
PropertyTranslator *gamesTranslator = [propertyTranslatorForClass:[Team class]
                                                     fromArrayKey:@"games"
                                                  toArrayProperty:@"games"
                                                        withClass:[Game class]];

[stone registerPropertyTranslator:numberTranslator];
[stone registerPropertyTranslator:gamesTranslator];

Team *piggers = [stone translate:teamDictionary toClass:[Team class]];

当您真正需要更改内容时

// Game.h

typedef enum : NSUInteger {
  PingPongGameType,
  FoosballGameType,
  TableHockeyGameType,
  NoGameType,
} GameType;

@interface Game

@property (nonatomic, copy) NSString *name;
@property (nonatomic) GameType type;

@end
// Elsewhere

#import <RosettaStoneKit/RosettaStoneKit.h>

NSDictionary *gameDictionary = @{
  @"name": @"FoosBall Championships",
  @"type": 2
};

RosettaStone *stone = [RosettaStone sharedInstance];
PropertyTranslator *gameTypeTranslator = [propertyTranslatorForClass:[Game class]
                                                             fromKey:@"type"
                                                           toProperty:@"type"
                                               withInterpolatorBlock:^id(id propertyValue) {
  NSUInteger number = [(NSNumber *)propertyValue unsignedIntegerValue];

  switch (number) {
    case 1:
      return PingPongGameType;
    case 2:
      return FoosballGameType;
    case 3:
      return TableHockeyGameType;
    default:
      return NoGameType;
  }
}];
[stone registerPropertyTranslator:gameTypeTranslator];

Game *foosballChampionshipGame = [stone translate:gameDictionary toClass:[Game class]];

将对象转换为字典

// User.h

@interface User

@property (nonatomic, copy)   NSString *firstName;
@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSDate *birthday;
@property (nonatomic, strong) NSString *lastName;

@end
// Elsewhere

#import <RosettaStoneKit/RosettaStoneKit.h>

User *joeUser = [User new];
joeUser.firstName = @"Joe";
joeUser.userId = @2;
joeUser.lastName = @"User";

RosettaStone *stone = [RosettaStone sharedInstance];

NSDictionary *joeUserDictionary = [stone translateToDictionary:joeUser];