Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将Web服务响应数据分配给NSManagedObject子类_Ios_Core Data_Nsdictionary_Nsmanagedobject_Key Value - Fatal编程技术网

Ios 将Web服务响应数据分配给NSManagedObject子类

Ios 将Web服务响应数据分配给NSManagedObject子类,ios,core-data,nsdictionary,nsmanagedobject,key-value,Ios,Core Data,Nsdictionary,Nsmanagedobject,Key Value,目前我正在对CoreData实体的所有属性逐一赋值,如下所示: employee.first_name = [responseDict valueForKey:@"first_name"]; employee.last_name = [responseDict valueForKey:@"last_name"]; employee.middle_name = [responseDict valueForKey:@"middle_name"]; // And So On... for say,

目前我正在对CoreData实体的所有属性逐一赋值,如下所示:

employee.first_name = [responseDict valueForKey:@"first_name"];
employee.last_name = [responseDict valueForKey:@"last_name"];
employee.middle_name = [responseDict valueForKey:@"middle_name"];

// And So On... for say, like 100+ more attributes.
问题:将要引入许多实体&我希望避免这些手动分配

问:有没有更聪明的方法来实现这一点

希望我能说清楚。如果需要任何信息,请告诉我。

如果您确定传入的字典键都存在于您的托管对象实体上,则可以使用SetValuesOfferKeyswithDictionary一步完成分配:

但我的意思是要确定。这将为responseDict中找到的每个键调用雇员上的setValue:forKey:on。如果responseDict包含任何雇员无法识别的密钥,您将得到一个异常,表明雇员不符合密钥的键值编码

如果您不确定,一种安全的方法是为employee创建一个NSManagedObject子类,并让该子类声明它希望从web服务接收的属性名称列表。例如,如果一个Employee类的NSArray如下所示:

self.webServicePropertyNames = @[ @"first_name", @"last_name", @"middle_name"];
…然后可以遍历该数组并使用setValue:forKey分配每个值,但使用通用代码


除此之外,您还可以使用Objective-C运行时查找员工的属性名称,然后在responseDict中找到它们。这将或多或少地像setValuesForKeysWithDictionary一样工作:除非没有尝试分配不存在的键值的危险。这不是小事,但。

我曾经有过这样一个案例:大约35个实体和数百个属性要移植到一个新的web API。我所做的是:

首先,我创建了映射字典作为plists,将webapi实体和属性名称与我的核心数据名称相匹配。这通常是必要的,因为web API有时使用id或描述等名称,这些名称是核心数据中的保留字

如果不需要数据类型验证,现在可以使用KVC分配如下属性

for (NSString *key in responseDictionary.allKeys) {
   NSString *entityKey = mappingDictionary[key];
   [entityInstance setValue:responseDictionary[key] forKey:entityKey];
}
如果您想比较数据类型,它会变得有点复杂。请看NSAttributedDescriptionAPI,它们将帮助您。它继承自NSPropertyDescription,其中包含与上述entityKey等效的name属性。您可以使用attributesByName从NSEntityDescription获取所有属性,或通过快速枚举获取包括关系在内的所有属性:

NSEntityDescription *anEntity = ...;
for (NSPropertyDescription *property in anEntity) {
    // property is each instance of NSPropertyDescription in anEntity in turn
}

您应该向Employee类添加自定义initWithDictionary方法,但基本上不需要。它必须使用相同的逻辑。您的问题是有许多不同的实体Employee、Department等,或者有许多不同的属性firstName、middleName等,或者Employee类有许多不同的对象即实例?或者三者的组合?问题是,我希望避免所有属性的这些赋值。像一些逻辑,它涉及所有的键和值,只需插入一条语句。希望我在这里讲得有道理。我在那里有数据类型验证,查看NSAttributedDescriptionAPI。感谢+1阅读您的博客文章。这似乎是一个有希望的解决方案+1.
NSEntityDescription *anEntity = ...;
for (NSPropertyDescription *property in anEntity) {
    // property is each instance of NSPropertyDescription in anEntity in turn
}