Xcode - Create csv/spreadsheet file

前端 未结 1 1126
忘了有多久
忘了有多久 2020-12-28 11:07

I have three arrays. The first array contains strings, same as the second, however the third one contains NSNumber\'s. Anyways, back on topic... how can I creat

相关标签:
1条回答
  • 2020-12-28 11:55
    
    NSArray *firstArray, *secondArray, *thirdArray;
    
    NSMutableString *csv = [NSMutableString stringWithString:@"Name,Date,Miles"];
    
    NSUInteger count = [firstArray count];
    // provided all arrays are of the same length
    for (NSUInteger i=0; i<count; i++ ) {
        [csv appendFormat:@"\n\"%@\",%@,\"%d\"",
            [firstArray objectAtIndex:i],
            [secondArray objectAtIndex:i],
            [[thirdArray objectAtIndex:i] integerValue]
         ];
        // instead of integerValue may be used intValue or other, it depends how array was created
    }
    
    NSString *yourFileName = @"your filename";
    NSError *error;
    BOOL res = [csv writeToFile:yourFileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
    
    if (!res) {
        NSLog(@"Error %@ while writing to file %@", [error localizedDescription], yourFileName );
    }
    
    
    0 讨论(0)
提交回复
热议问题