On 19/08/2009, at 7:27 AM, Michael A. Crawford wrote:

So, I'm looking for suggestions as to the best way to add this feature. I could iterate through the objects in the array and use some posix code to write the data out in CSV format but there must be a better, more Cocoa oriented, way.

Any one have a brilliant idea that leverages Cocoa in a way that will be relatively quick and easy?


I added code to write out data to a tab-delimited file recently, and I did this by adding a simple method to my controller:

- (NSString*) dataAsTabDelimitedString;

Internally it does the following (in my case):

- (NSString*)   dataAsTabDelimitedString
{
// returns the current data content of the array as a tab-delimited string. The string is formatted into lines with each line representing a row. Each // element in the array contributes by supplying its value using the - stringValue method.
        
        unsigned                j, k;
        NSMutableString*        string = [NSMutableString string];
        id                      object;
        
        for( j = 0; j < [self rows]; ++j )
        {
                for( k = 0; k < [self columns]; ++k )
                {
                        object = [self objectAtRow:j column:k];
                        [string appendFormat:@"%...@\t", [object stringValue]];
                }
                [string appendString:@"\n"];
        }
        
        return string;
}

This relies on each data object returning something useful in its - stringValue method, which depending on what your data is, you might need to write yourself. Some classes have this method already though, like NSNumber.

Once you have the data as a string, you can write it to a file using NSString's -writeToFile:atomically: method.


--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to