可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to write data from sql file to csv file. I have collected all data from sql file in an array and using for loop i am appending and writing data to .csv file. but it seems that it shows data in one line only it does not go to new line to create new row. I have used this for reference. This is my code :
-(NSString *)dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:@"myfile.csv"]; } - (IBAction)saveAsFileAction:(id)sender { if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) { [[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil]; NSLog(@"Route creato"); } NSString *writeString; for (int i=0; i
回答1:
This only writes one line because you rewrite the file every time you go through your loop. It is best to not writeData on the file until the loop has completed. I would also use an NSMutableString like this:
- (IBAction)saveAsFileAction:(id)sender { if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) { [[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil]; NSLog(@"Route creato"); } NSMutableString *writeString = [NSMutableString stringWithCapacity:0]; //don't worry about the capacity, it will expand as necessary for (int i=0; i
回答2:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSString *root = [documentsDir stringByAppendingPathComponent:@"customers.csv"]; NSString *temp; temp = [NSString stringWithFormat:@"%@", [arrCustomersName objectAtIndex:0]]; for (int i = 1; i
回答3:
Instead of :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSString *root = [documentsDir stringByAppendingPathComponent:@"customers.csv"];
... just write :
NSString *root = [NSHomeDirectory() stringByAppendingPathComponent:@"customers.csv"];
Exactly same result for only one short line of code. ;o)
回答4:
// For CSV File : NSMutableString *stringToWrite = [[NSMutableString alloc] init]; [stringToWrite appendString:[NSString stringWithFormat:@"First Name,Last Name,Full Name,Phone Number, Email,Job, organizationName,Note\n\n"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for(int i = 0 ;i
回答5:
Try this it's working for me ,
If any one want to create .csv file in swift 3
// MARK: CSV file creating func creatCSV() -> Void { let fileName = "Tasks.csv" let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName) var csvText = "Date,Task Name,Time Started,Time Ended\n" for task in taskArr { let newLine = "\(task.date),\(task.name),\(task.startTime),\(task.endTime)\n" csvText.append(newLine) } do { try csvText.write(to: path!, atomically: true, encoding: String.Encoding.utf8) } catch { print("Failed to create file") print("\(error)") } print(path ?? "not found") } }
For more details you can refer Detail Answer
Hopes this will help to some one .