pEpObjCAdapter/NSObject+Extension.m
author Dirk Zimmermann <dz@pep.security>
Thu, 21 Mar 2019 12:37:32 +0100
branchrefactor
changeset 903 e9428f52fd88
parent 593 78a14e6e06d0
permissions -rw-r--r--
IOS-1480 Merge IOS-1480 into refactor.
     1 //
     2 //  NSObject+Extension.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Dirk Zimmermann on 24.04.18.
     6 //  Copyright © 2018 p≡p. All rights reserved.
     7 //
     8 
     9 #import "NSObject+Extension.h"
    10 
    11 @implementation NSObject (Extension)
    12 
    13 - (BOOL)isEqualToObject:(NSObject * _Nonnull)other
    14             basedOnKeys:(NSArray<NSString *> * _Nonnull)keys
    15 {
    16     for (NSString *theKey in keys) {
    17         NSObject *objSelf = [self valueForKey:theKey];
    18         NSObject *objOther = [other valueForKey:theKey];
    19 
    20         if (objSelf == nil && objOther == nil) {
    21             // considered equal, continue
    22         } else if (![objSelf isEqual:objOther]) {
    23             // NSValue, NSArray, NSString all have correctly implemented isEqual, so this works
    24             return NO;
    25         }
    26     }
    27 
    28     return YES;
    29 }
    30 
    31 - (NSUInteger)hashBasedOnKeys:(NSArray<NSString *> * _Nonnull)keys
    32 {
    33     NSUInteger prime = 31;
    34     NSUInteger result = 1;
    35 
    36     for (NSString *theKey in keys) {
    37         NSObject *objSelf = [self valueForKey:theKey];
    38         result = prime * result + objSelf.hash;
    39     }
    40 
    41     return result;
    42 }
    43 
    44 @end