@mainpage DictionaryWrapper
此工具包提供了一种创建支持键值索引界面的对象的包装类的高速方法。它特别适用于 JSON 处理。
注意:此工具包通过子类化工作。请首先阅读下面的文档。
Cocoapods 可用。
#import <DictionaryWrapper/DictionaryWrapper.h>
//! Header
/*!
* Subclass @ref DWObject to make your own interface for your dictionaries.
*/
@interface DWTest: DWObject
/*!
* Declare properties to access. Tip: readonly is automatically supported.
*/
@property(readonly) NSString *type;
@property(readonly) NSString *value;
@property(readonly) NSNumber *number;
@property(readonly) NSNull *null;
@end
//! Body
@implementation DWTest
/*!
* you need @dynamic to prevent auto-synthesizing
*/
@dynamic type, value, number, null;
@end
//! Header
/*!
* DWNotNullObject is same to DWObject, except for the NSNull handling.
* Use it for your preferences.
*/
@interface DWSafeTest : DWNotNullObject
/*!
* Declare properties to access. Tip: readonly is automatically supported.
*/
@property(readonly) NSString *type;
@property(readonly) NSString *value;
@property(readonly) NSNumber *number;
@property(readonly) NSNull *null;
@end
//! Body
@implementation DWSafeTest
/*!
* you need @dynamic to prevent auto-synthesizing
*/
@dynamic type, value, number, null;
@end
//! Usage
void test(id self) {
// preparing JSON data and object
NSData *JSONData = [@"{\"type\": \"test\", \"value\": \"wrapped value\", \"number\": 42, \"null\": null}" dataUsingEncoding:NSUTF8StringEncoding];
id dataObject = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:NULL];
DWTest *testObject = [DWTest :dataObject]; // convinient method to create DWObject
XCTAssertEqualObjects(testObject.type, @"test", @"`type` property is connected to the field in JSON data");
XCTAssertEqualObjects(testObject.value, @"wrapped value", @"`value` property is also connected");
XCTAssertEqualObjects(testObject.number, @(42), @"`number` is type of NSNumber");
XCTAssertEqualObjects(testObject.null, [NSNull null], @"null is type of NSNull");
DWSafeTest *safeObject = [DWSafeTest :dataObject]; // almost same
XCTAssertNil(safeObject.null, @"but `null` is nil");
NSArray *dataObjects = @[dataObject, dataObject, dataObject];
NSArray *multipleObjects = [DWTest objectsWithDataObjects:dataObjects];
XCTAssertEqual(multipleObjects.count, dataObjects.count, @"conut is the same");
for (id object in multipleObjects) {
XCTAssertEqualObjects([object class], [DWTest class], @"classes are the same");
}
XCTAssertEqualObjects(multipleObjects[0], multipleObjects[1], @"if data are the same, wrappers also are the same.");
}
关于初始化器的信息,请参阅
@see DWObject