我整天都在使用这里的答案来解决这个问题,但我找不到适合我的解决方案,所以我想我会寻求帮助:
我有一组从实体中提取的对象。
我要做的就是将对象写入文件。
到目前为止,我想出的是:
NSLog(@" Exported Records: %i", [exportArray count]);
// the count here is 4 records.
//Each record has about 8 elements.
//I'm just trying to get this working with the first two elements right now
NSString *writeString = nil;
NSError *error = nil;
int i = 0;
NSString *key = nil;
NSString *tempString = nil;
for (i=0; i<[exportArray count]; i++)
{
tempString = [tempString stringByAppendingString: @" \n "];
for (int j=0; j<3; j++)
{
if (j == 0)
{
key = [[exportArray objectAtIndex:i] valueForKey:@"title"];
//[NSString stringWithFormat:tempString2, [[exportArray objectAtIndex:i] valueForKey:@"title"]];
}
if (j == 1)
{
key = [[exportArray objectAtIndex:i] valueForKey:@"username"];
//[NSString stringWithFormat:tempString2, [[exportArray objectAtIndex:i] valueForKey:@"username"]];
}
writeString = [NSString stringWithFormat:tempString, key];
}
}
// Write to the file
[writeString writeToFile:dataFile atomically:YES
encoding:NSUTF8StringEncoding error:&error];
if (error)
{
NSLog(@"%@", error);
}
现在,我得到了最后一项,所以我覆盖了字符串。但是,必须有更好的方法来实现这一点。如果这里有另一个答案与我的问题相匹配,请告诉我,或者请发表一些想法。
更新
当我记录 exportArray 时,我得到了这个:
"<ItemEntity: 0x1ddf6560> (entity: ItemEntity; id: 0x1ddf46d0 <x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p2> ; data: <fault>)",
"<ItemEntity: 0x1ddf6800> (entity: ItemEntity; id: 0x1ddf46e0 <x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p2051> ; data: <fault>)",
"<ItemEntity: 0x1ddf6860> (entity: ItemEntity; id: 0x1ddf45d0 <x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p3075> ; data: <fault>)",
"<ItemEntity: 0x1ddf68c0> (entity: ItemEntity; id: 0x1ddf45e0 <x-coredata://78FBC4A5-AE1A-4344-98AB-978126457D96/ItemEntity/p5124> ; data: <fault>)"
)
当我记录实际值时:
NSLog(@" Exported titles: %@", [exportArray valueForKey:@"title"]);
NSLog(@" Exported usernames: %@", [exportArray valueForKey:@"username"]);
我得到正确的结果。我只是不知道如何将这些和其他属性联系在一起..
Exported titles: (
1,
2,
4,
6
)
Exported usernames: (
ellis,
david,
bea,
ian
)