为NsObject键 - 表达式不可分配添加值

发布于 2025-01-24 11:21:41 字数 957 浏览 2 评论 0原文

我正在尝试添加一个从API调用到Objective-C中的对象的值。我在Xcode中遇到了一个错误,说“表达是不可分配的”,不明白为什么。在我的carobj中,我有一个名为保证计划的对象,带有nil值,我正在尝试为prants> prants> prants> prants> prants> prants> pranantsplan value设置值代码>。我在这种方法中做错了什么?

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;
        }];
    }

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;
        }];
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2025-01-31 11:21:41

[Carobj valueforkeypath:@“ warrantyplans”] expression ,其具有类似于42的值是具有值的表达式。它不是foo的表达式,它的变量为42,可以通过在平等符号的左侧使用它来更改。

要更改它,您想要:

[Carobj SetValue:响应forkeypath:@“ prantyplans”]

这两个称为通用 getter valueforkey: and code>通用 setter setValue:forkey:在KEYVALUECODING指南中: https://developer.apple.com/library/archive/archive/documentation/cocoa/cocoa/coneptual/coneptual/keyvaluecoding/index.htex.html

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文