开发者

How do I get Core Data data into an array of arrays? For use with Dave DeLong's CHCSVWriter

开发者 https://www.devze.com 2023-02-15 15:24 出处:网络
I think this is pretty simple but i\'m new to Objective-C, IOS and Core Data. I have a Core Data entity called EnquiryStore, which contains various attributes \"name\", \"address\", \"enquiry\" etc..

I think this is pretty simple but i'm new to Objective-C, IOS and Core Data.

I have a Core Data entity called EnquiryStore, which contains various attributes "name", "address", "enquiry" etc..

I can fetch the results fine but I strugg开发者_C百科ling to understand how the results are stored! Which is making it hard for me to do the next part of my program which is to export the data to CSV using Dave DeLong's CHCSVWriter. I understand that to use

[array writeToCSVFile:outputCSVFile atomically: NO];

It will accept an Array of Arrays, is this what Core Data gives me? Because i can't get it to work with that method.

I could use

[myCSVWriter writeField: ...];
[myCSVWriter writeLine];

but this seems long winded as I will have to build the string from each attribute from Core Data.


You are the only person that knows how your NSManagedObject looks like, it could have relationships which would be impossible to put in a csv file. It could have binary data which would not be very useful in a csv file. Or it just could have attributes that you don't want to export. And you have to decide in which order you want your csv exported too.

It's easy to create a NSArray (ie the list of NSManagedObjects you want to export) of NSArray (ie the list of exported attributes for that object). Just enumerate through all your NSManagedObject and fill an array with the values you want to export.

You could use something like this:

NSArray *objectsForExport = ... /* get it through a fetch request... helper function... whatever */
NSArray *exportKeys = [NSArray arrayWithObjects:@"foo", @"bar", @"name", @"city", @"address", nil];

NSMutableArray *csvObjects = [NSMutableArray arrayWithCapacity:[objectsForExport count]];
for (NSManagedObject *object in objectsForExport) {
    NSMutableArray *anObjectArray = [NSMutableArray arrayWithCapacity:[exportKeys count]];
    for (NSString *key in exportKeys) {
        id value = [object valueForKey:key];
        if (!value) {
            value = @"";
        }
        [anObjectArray addObject:[value description]];
    }
    [csvObjects addObject:anObjectArray];
}

and there you have your array of arrays.

I believe there was a method that returned an NSArray of values if you pass it a NSArray of keys, but I can not find it, and I can't remember its name or even to which class it belonged.


While the "writeField:" approach may seem long winded, it's also the most descriptive:

CHCSVWriter *writer = [[CHCSVWriter alloc] initWithCSVFile:@"/Path/To/CSV/File.csv" atomically:NO];

NSArray *myArrayOfManagedObjects = ...;
for (NSManagedObject *obj in myArrayOfManagedObjects) {
  [writer writeField:[obj valueForKey:@"attribute1"]];
  [writer writeField:[obj valueForKey:@"attribute2"]];
  [writer writeField:[obj valueForKey:@"attribute3"]];
  [writer writeField:[obj valueForKey:@"attribute4"]];
  [writer writeLine];
}

[writer closeFile];
[writer release];

Note that writeField: takes an id as the parameter, not an NSString*. You can pass it any object that you want, and CHCSVWriter will grab the object's -description and write that out. (This even happens with NSString objects that are passed in) This happens because some modifications to the value have to take place (such as determining if the field needs quoting).

0

精彩评论

暂无评论...
验证码 换一张
取 消