Search code examples
swiftswift-extensions

keyPathsForValuesAffecting with NSManagedObject


Bonjour, I would like to translate an objective'c exercise from an Aaron's book to swift but I can't find the solution. The Objective'c code is :

@dynamic firstName;
@dynamic lastName;
@dynamic department;

+ (NSSet *)keyPathsForValuesAffectingFullName
{
    return [NSSet setWithObjects:@"firstName", @"lastName", nil];
}

- (NSString *)fullName
{
    NSString *first = [self firstName];
    NSString *last = [self lastName];
    if (!first)
        return last;
    if (!last)
        return first;
    return [NSString stringWithFormat:@"%@ %@", first, last];
}

I found a function in the developer documentation but I can't understand how to implement this code.

to be more explicit, this is the Apple doc

To-one Relationships

To trigger notifications automatically for a to-one relationship you should either override keyPathsForValuesAffectingValueForKey: or implement a suitable method that follows the pattern it defines for registering dependent keys.

For example, the full name of a person is dependent on both the first and last names. A method that returns the full name could be written as follows:

- (NSString *)fullName {
    return [NSString stringWithFormat:@"%@ %@",firstName, lastName];
}

An application observing the fullName property must be notified when either the firstName or lastName properties change, as they affect the value of the property.

One solution is to override keyPathsForValuesAffectingValueForKey: specifying that the fullName property of a person is dependent on the lastName and firstName properties. Listing 1 shows an example implementation of such a dependency:

Listing 1 Example implementation of keyPathsForValuesAffectingValueForKey:

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {

    NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];

    if ([key isEqualToString:@"fullName"]) {
        NSArray *affectingKeys = @[@"lastName", @"firstName"];
        keyPaths = [keyPaths setByAddingObjectsFromArray:affectingKeys];
    }
    return keyPaths;
}

class func keyPathsForValuesAffectingValueForKey(_ key: String) -> NSSet

Can somebody tell me how implement this function in swift?

Thank you for helping me.


Solution

  • I found the solution of my problem! Just override the func keyPathsForValuesAffectingValueForKey(key: String) with class before

    Here the code:

    class Locataires: NSManagedObject {
    @NSManaged var firstName: String
    @NSManaged var lastName: String
    var fullName: NSString {
        get {
     return firstName + lastName
        }
    }
    
    override class func keyPathsForValuesAffectingValueForKey(key: String) -> NSSet {
        if key == «fullName « {
            let mesClefs = ["firstName", "lastName"]
            return NSSet(array: mesClefs)
        }
        else {
            return super.keyPathsForValuesAffectingValueForKey(key)
        }
    }
    

    Thanks for the help Jan