将对象转换成可以发送到 Web 服务的字典的小型库。
与字符串、数字和日期兼容良好。其他数据需要转换。默认情况下,日期使用 rails 日期时间格式解析。
Item *item = [[Item alloc] init];
item.title = @"Title";
item.itemID = [NSNumber numberWithInt:15];
item.createdAt = [NSDate date];
[BWObjectSerializerMapping mappingForObject:[Item class] block:^(BWObjectSerializerMapping *serializer) {
[serializer mapKeyPath:@"title" toAttribute:@"title"];
[serializer mapKeyPath:@"itemID" toAttribute:@"id"];
[serializer mapKeyPath:@"imagePath" toAttribute:@"image_url" valueBlock:^id(id value, id object) {
return @"custom value";
}];
[serializer mapKeyPath:@"createdAt" toAttribute:@"created_at" withDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[[BWObjectSerializer shared] registerSerializer:serializer withRootKeyPath:@"item"];
}];
NSDictionary *dict = [[BWObjectSerializer shared] serializeObject:item];
或者
NSDictionary *dict [[BWObjectSerializer shared] serializeObject:item withMapping:mapping];
将生成以下字典
{
item = {
"created_at" = "2012-07-20T21:56:26Z";
"image_url" = "custom value";
title = Title;
};
}
BWStatusBarOverlay 仅支持 ARC。
Bruno Wernimont