将数组内容写入文件 [英] Writing array contents to a file

查看:83
本文介绍了将数组内容写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在使用这里的答案来解决这个问题,但我找不到适合我的解决方案,所以我想我会寻求帮助:

I've struggled with this all day using answers from here, but I cannot find a solution that is working for me, so I thought I'd ask for help:

我有一个从实体中提取的对象数组.

I have an array of objects that I've extracted from an entity.

我想做的就是将对象写入文件.

All I want to do is to write the objects to a file.

到目前为止我想出的是:

What I've come up with so far is this:

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

现在,我只得到最后一项,所以我要覆盖字符串.但是,必须有更好的方法来实现这一点.如果这里有其他答案与我的问题相符,请告诉我,或者请发表一些想法.

Right now, all I'm getting the the last item, so I'm overwriting the string. But, there must be a better method to achieve this. Please let me know if there is another answer here matches my question, or please, post some ideas.

更新

当我记录 exportArray 时,我得到这个:

When I log the exportArray, I get this:

"<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"]);

我得到了正确的结果.我只是不知道如何将这些和其他属性联系在一起..

I get correct results. I just don't know how to tie these and the other attributes together..

  Exported titles: (
    1,
    2,
    4,
    6
)
  Exported usernames: (
    ellis,
    david,
    bea,
    ian
)

推荐答案

如果你的数组中只有 NSDictionaries,你可以使用 writeToFile:atomically: 这将节省.plist 格式的文件(比 CSV 更容易阅读).

If you just have NSDictionaries in your array, you can use writeToFile:atomically: which will save a file in .plist format (which is much easier to read than a CSV).

查看 Apple 文档用于 NSArray,以及关于 plist 的好文章.

Check out the Apple Docs for NSArray, and a good write up on plists.

所以你最终会得到:

[exportArray writeToFile:fileName atomically:YES];

<小时>

您无法将核心数据对象保存到文件中.因此,您要做的是创建一个仅包含您想要的数据的临时数组,然后将其写入文件.


You cannot save a core data object to a file. So what you want to do is create an interim array with just the data you want, then write that to the file.

NSMutableArray* arrayToSave = [NSMutableArray arrayWithCapacity:exportArray.count];
[exportArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSDictionary* exportDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                [obj objectForKey:@"username"], @"username",
                                [obj objectForKey:@"title"], @"title", nil];
    [arrayToSave addObject:exportDict];
}];

[arrayToSave writeToFile:fileName atomically:YES];

这篇关于将数组内容写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆