react-native-navigation的迁移库

RNNOptions.m 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 *)mergeInOptions:(RNNOptions *)otherOptions {
  30. if (!otherOptions) {
  31. return self;
  32. }
  33. return [otherOptions mergeOptions:self overrideOptions:NO];
  34. }
  35. - (RNNOptions *)withDefault:(RNNOptions *)defaultOptions {
  36. RNNOptions* newOptions = [[[self class] alloc] initWithDict:@{}];
  37. [newOptions mergeOptions:defaultOptions overrideOptions:YES];
  38. [newOptions mergeOptions:self overrideOptions:YES];
  39. return newOptions;
  40. }
  41. - (NSArray *)objectProperties:(NSObject *)object {
  42. NSMutableArray* properties = [NSMutableArray new];
  43. unsigned int count;
  44. objc_property_t* props = class_copyPropertyList([object class], &count);
  45. for (int i = 0; i < count; i++) {
  46. objc_property_t property = props[i];
  47. NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
  48. [properties addObject:propertyName];
  49. }
  50. free(props);
  51. return properties;
  52. }
  53. @end