酷店

MJPropertyType.m 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // MJPropertyType.m
  3. // MJExtension
  4. //
  5. // Created by mj on 14-1-15.
  6. // Copyright (c) 2014年 小码哥. All rights reserved.
  7. //
  8. #import "MJPropertyType.h"
  9. #import "MJExtension.h"
  10. #import "MJFoundation.h"
  11. #import "MJExtensionConst.h"
  12. @implementation MJPropertyType
  13. + (instancetype)cachedTypeWithCode:(NSString *)code
  14. {
  15. MJExtensionAssertParamNotNil2(code, nil);
  16. static NSMutableDictionary *types;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. types = [NSMutableDictionary dictionary];
  20. });
  21. MJPropertyType *type = types[code];
  22. if (type == nil) {
  23. type = [[self alloc] init];
  24. type.code = code;
  25. types[code] = type;
  26. }
  27. return type;
  28. }
  29. #pragma mark - 公共方法
  30. - (void)setCode:(NSString *)code
  31. {
  32. _code = code;
  33. MJExtensionAssertParamNotNil(code);
  34. if ([code isEqualToString:MJPropertyTypeId]) {
  35. _idType = YES;
  36. } else if (code.length == 0) {
  37. _KVCDisabled = YES;
  38. } else if (code.length > 3 && [code hasPrefix:@"@\""]) {
  39. // 去掉@"和",截取中间的类型名称
  40. _code = [code substringWithRange:NSMakeRange(2, code.length - 3)];
  41. _typeClass = NSClassFromString(_code);
  42. _fromFoundation = [MJFoundation isClassFromFoundation:_typeClass];
  43. _numberType = [_typeClass isSubclassOfClass:[NSNumber class]];
  44. } else if ([code isEqualToString:MJPropertyTypeSEL] ||
  45. [code isEqualToString:MJPropertyTypeIvar] ||
  46. [code isEqualToString:MJPropertyTypeMethod]) {
  47. _KVCDisabled = YES;
  48. }
  49. // 是否为数字类型
  50. NSString *lowerCode = _code.lowercaseString;
  51. NSArray *numberTypes = @[MJPropertyTypeInt, MJPropertyTypeShort, MJPropertyTypeBOOL1, MJPropertyTypeBOOL2, MJPropertyTypeFloat, MJPropertyTypeDouble, MJPropertyTypeLong, MJPropertyTypeLongLong, MJPropertyTypeChar];
  52. if ([numberTypes containsObject:lowerCode]) {
  53. _numberType = YES;
  54. if ([lowerCode isEqualToString:MJPropertyTypeBOOL1]
  55. || [lowerCode isEqualToString:MJPropertyTypeBOOL2]) {
  56. _boolType = YES;
  57. }
  58. }
  59. }
  60. @end