为NsObject键 - 表达式不可分配添加值
我正在尝试添加一个从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[Carobj valueforkeypath:@“ warrantyplans”]
是 expression ,其具有类似于42
的值是具有值的表达式。它不是foo
的表达式,它的变量为42,可以通过在平等符号的左侧使用它来更改。要更改它,您想要:
[Carobj SetValue:响应forkeypath:@“ prantyplans”]
这两个称为通用 getter
valueforkey:
and code>通用 settersetValue: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 literally42
is an expression that has a value. It's not an expression likefoo
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 settersetValue:forKey:
in the KeyValueCoding guide: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html