//
//  ObjectConver.m
//  ziizaa
//
//  Created by FoocaaThomas on 2018/7/18.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "ObjectConver.h"
#import <objc/runtime.h>

@implementation ObjectConver


+ (NSDictionary *) entityToDictionary:(id)entity
{
  
  Class clazz = [entity class];
  u_int count;
  
  objc_property_t* properties = class_copyPropertyList(clazz, &count);
  NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
  NSMutableArray* valueArray = [NSMutableArray arrayWithCapacity:count];
  
  for (int i = 0; i < count ; i++)
  {
    objc_property_t prop=properties[i];
    const char* propertyName = property_getName(prop);
    
    [propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
  
    
    id value = [entity valueForKey:[NSString stringWithUTF8String:propertyName]];
    if(value ==nil)
      [valueArray addObject:[NSNull null]];
    else {
      [valueArray addObject:value];
    }
  }
  
  free(properties);
  
  NSDictionary* returnDic = [NSDictionary dictionaryWithObjects:valueArray forKeys:propertyArray];
  
  return returnDic;
}

@end
