NSDictionary+Safe.m 821 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // NSDictionary+Safe.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2023/11/7.
  6. //
  7. #import "NSDictionary+Safe.h"
  8. #import "NSObject+ImpChangeTool.h"
  9. #import <objc/runtime.h>
  10. @implementation NSDictionary (Safe)
  11. + (void)load{
  12. [self SwizzlingMethod:@"initWithObjects:forKeys:count:" systemClassString:@"__NSPlaceholderDictionary" toSafeMethodString:@"initWithObjects_st:forKeys:count:" targetClassString:@"NSDictionary"];
  13. }
  14. -(instancetype)initWithObjects_st:(id *)objects forKeys:(id<NSCopying> *)keys count:(NSUInteger)count {
  15. NSUInteger rightCount = 0;
  16. for (NSUInteger i = 0; i < count; i++) {
  17. if (!(keys[i] && objects[i])) {
  18. break;
  19. }else{
  20. rightCount++;
  21. }
  22. }
  23. self = [self initWithObjects_st:objects forKeys:keys count:rightCount];
  24. return self;
  25. }
  26. @end