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