ios - 将数据从 NSArray 加载到 NSManagedObject
<p><p>是否可以将数据从 .plist 加载到核心数据对象?
我有这个代码:</p>
<pre><code>NSURL *url = [ URLForResource:@"List" withExtension:@"plist"];
NSDictionary *plistContent = ;
NSArray *name = ;
NSArray *surname = ;
NSManagedObjectContext *context = ;
NSEntityDescription *entity = [ entity];
NSManagedObject *newManagedObject = inManagedObjectContext:context];
for (NSString *aName in name) {
for (int i = 0; i < name.count; i++) {
;
}
}
for (NSString *aSurname in content) {
for (int i = 0; i < content.count; i++) {
;
}
}
;
</code></pre>
<p>我有 3 个带名字的字符串和 3 个带姓氏的字符串,但是在
newManagedObject 仅从每个数组中加载 1 个字符串。请帮我解决这个问题</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您只创建一个对象,但您应该创建 3 个(在循环内)。你也应该只有 1 个循环(不是你目前拥有的 4 个......):</p>
<pre><code>NSURL *url = [ URLForResource:@"List" withExtension:@"plist"];
NSDictionary *plistContent = ;
NSArray *names = ;
NSArray *surnames = ;
if (names.count == surnames.count) {
NSManagedObjectContext *context = ;
NSEntityDescription *entity = [ entity];
for (int i = 0; i < names.count; i++) {
NSString *name = names;
NSString *surname = surnames;
NSManagedObject *newManagedObject = inManagedObjectContext:context];
;
;
}
;
} else {
// error...
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将数据从 NSArray 加载到 NSManagedObject,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28914323/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28914323/
</a>
</p>
页:
[1]