react-native-navigation的迁移库

RNNOptions.m 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #import "RNNOptions.h"
  2. #import <objc/runtime.h>
  3. @implementation RNNOptions
  4. - (instancetype)initWithDict:(NSDictionary *)dict {
  5. self = [super init];
  6. return self;
  7. }
  8. - (RNNOptions *)mergeOptions:(RNNOptions *)otherOptions overrideOptions:(BOOL)override {
  9. for (id prop in [self objectProperties:otherOptions]) {
  10. id value = [otherOptions valueForKey:prop];
  11. if ([value isKindOfClass:[RNNOptions class]]) {
  12. [[self valueForKey:prop] mergeOptions:value overrideOptions:override];
  13. } else if ([value isKindOfClass:[Param class]]) {
  14. if ((((Param *)value).hasValue) && (override || !((Param *)[self valueForKey:prop]).hasValue)) {
  15. [self setValue:value forKey:prop];
  16. }
  17. } else if (value && (override || ![self valueForKey:prop])) {
  18. [self setValue:value forKey:prop];
  19. }
  20. }
  21. return self;
  22. }
  23. - (RNNOptions *)overrideOptions:(RNNOptions *)otherOptions {
  24. return [self mergeOptions:otherOptions overrideOptions:YES];
  25. }
  26. - (RNNOptions *)mergeOptions:(RNNOptions *)otherOptions {
  27. return [self mergeOptions:otherOptions overrideOptions:NO];
  28. }
  29. - (RNNOptions *)withDefault:(RNNOptions *)defaultOptions {
  30. RNNOptions* newOptions = [[[self class] alloc] initWithDict:@{}];
  31. [newOptions mergeOptions:defaultOptions overrideOptions:YES];
  32. [newOptions mergeOptions:self overrideOptions:YES];
  33. return newOptions;
  34. }
  35. - (NSArray *)objectProperties:(NSObject *)object {
  36. NSMutableArray* properties = [NSMutableArray new];
  37. unsigned int count;
  38. objc_property_t* props = class_copyPropertyList([object class], &count);
  39. for (int i = 0; i < count; i++) {
  40. objc_property_t property = props[i];
  41. NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  42. [properties addObject:propertyName];
  43. }
  44. free(props);
  45. return properties;
  46. }
  47. @end