https://github.com/douban/polymorph
Transform value of dictionary to property of Objective-C class, by using a `dynamic` like directive.
https://github.com/douban/polymorph
Last synced: 3 months ago
JSON representation
Transform value of dictionary to property of Objective-C class, by using a `dynamic` like directive.
- Host: GitHub
- URL: https://github.com/douban/polymorph
- Owner: douban
- License: bsd-3-clause
- Created: 2016-01-25T06:55:18.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-12-27T11:04:58.000Z (9 months ago)
- Last Synced: 2025-07-03T11:02:03.886Z (3 months ago)
- Language: Objective-C
- Homepage:
- Size: 145 KB
- Stars: 40
- Watchers: 15
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Polymorph
[](https://github.com/douban/Polymorph/blob/master/LICENSE)
[](https://cocoapods.org/pods/Polymorph)
[](https://cocoapods.org/pods/Polymorph)
[](https://travis-ci.org/douban/Polymorph)
[](https://codecov.io/github/douban/Polymorph)> [Polymorph](http://wowwiki.wikia.com/wiki/Polymorph) transforms the enemy into a sheep.
Transform value of dictionary to property of Objective-C class, by using a `@dynamic` like directive.
## Usage
Say we have a `Movie` class.
```objc
@interface Movie : PLMModel@property (nonatomic, readonly) NSString *identifier;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *year;
@property (nonatomic, readonly) NSString *subtype;
@property (nonatomic, readonly) float rating;
@property (nonatomic, readonly) NSArray *casts;@end
```Instead of implementing accessor methods for each property, we can use `plm_dynamic` macro to generate getter and setter automatically.
```objc
@implementation Movie// Property `identifier` comes from `id` field。
@plm_dynamic(identifier, @"id")// Property `title` comes from field with same name `title`.
@plm_dynamic(title)// `year` and `subtype` comes from fields with same names.
@plm_dynamic_multi(year, subtype)// `rating` comes from `rating.average` keypath. Field value will be transformed to `float` as it's declared.
@plm_dynamic_keypath(rating, @"rating.average")// `casts` comes from `casts` field. Field value, which is an object array, will be transformed to NSArray with Celebrity instance.
@plm_dynamic(casts, @"casts", PLMArrayTransformerNameForClass([Celebrity class]))@end
````plm_dynamic` macro associate property and dictionary field, use `NSValueTransformer` to transform dictionary value to declared type. See comments in `Polymorph.h` for detailed usage.
### Without inheritance
You can also use Polymorph without extending `PLMModel`. Conform to `PLMRawDataProvider` protocol, invoke `plm_activate`, and you are ready to go.
## License
Polymorph is released under BSD license. See [LICENSE](https://github.com/douban/Polymorph/blob/master/LICENSE) for more.