Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding value to NSObject key- Expression is not assignable

I’m trying to add a value I get back from an API call to an object in objective C. I get an error in Xcode saying “Expression is not assignable” and don’t understand why. In my carObj I have an object called warrantyPlan with a nil value and I’m trying to set the value for warrantyPlan. What am I doing wrong in this method?

NSArray *carUUIDs = [carData valueForKeyPath:@"uuid"];
                        NSString *ownerUUID = ownerRecord[@"uuid"];
                        if (ownerID) {
                            NSManagedObjectContext *context = [DataCenter viewContext];
                            NSObject *carObj = [context objectsForEntityName:[CarObject entityName] matchingUUIDs:carUUIDs];
                            
                            for (id carID in carUUIDs){
                                [WarrantyPlansService getWarrantyPlansForCarWithCarID:carID ownerID:ownerUUID completion:^(NSArray* response, NSError* error){
                                    //attach the response to the carData
                                    NSLog(@"%@", [carObj valueForKeyPath:@"warrantyPlans"]);
                                    [carObj valueForKeyPath:@"warrantyPlans"] = response;
                                }];
                            }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[carObj valueForKeyPath:@"warrantyPlans"] is an expression that has a value like literally 42 is an expression that has a value. It’s not an expression like foo that is a variable that has a value of 42 and can be changed by using it on the left hand side of an equals sign.

To change it you want:

[carObj setValue:response forKeyPath:@"warrantyPlans"]

These two are called the generic getter valueForKey: and the generic setter setValue:forKey: in the KeyValueCoding guide: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading