From 8ac0ec6bde4da701d06ffc8387a25759a13d485c Mon Sep 17 00:00:00 2001 From: Vignesh Date: Tue, 7 Nov 2017 21:03:48 +0530 Subject: [PATCH] - Updated README file. - Handled the punctuation(\n, \t, \t, and ,) characters in CSV file. - Updated the SampleSwift with Examples. --- .../SampleSwift/ViewController.swift | 5 ++++- README.md | 5 ++++- SwiftCSVExport.podspec | 2 +- SwiftCSVExport/Sources/CSVExport.swift | 18 ++++++++++++++---- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Examples/SampleSwift/SampleSwift/ViewController.swift b/Examples/SampleSwift/SampleSwift/ViewController.swift index 320a290..c926334 100644 --- a/Examples/SampleSwift/SampleSwift/ViewController.swift +++ b/Examples/SampleSwift/SampleSwift/ViewController.swift @@ -29,16 +29,19 @@ class ViewController: UIViewController { let user1:NSMutableDictionary = NSMutableDictionary() user1.setObject("vignesh", forKey: "name" as NSCopying); user1.setObject("vigneshuvi@gmail.com", forKey: "email" as NSCopying); + user1.setObject("Hi Vignesh, \nhow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "address" as NSCopying); let user2:NSMutableDictionary = NSMutableDictionary() user2.setObject("vinoth", forKey: "name" as NSCopying); user2.setObject("vinoth@gmail.com", forKey: "email" as NSCopying); + user2.setObject("Hi Vinoth, \nHow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "address" as NSCopying); + let data:NSMutableArray = NSMutableArray() data.add(user1); data.add(user2); - let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email"],values: data); + let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email", "address"],values: data); print(filePath) let request = NSURLRequest(url: URL(fileURLWithPath: filePath) ) diff --git a/README.md b/README.md index 7a46b31..e81455b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Simple way to export csv file with rich feature framework and written in Swift. - Support CocoaPods, mac OS and Vapor framework(Swift Package Manager). - Able to encoding CSV based on String.Encoding Type(utf8, ascii, unicode, utf16, etc) Refer: String.Encoding. - Able to view the exported CSV documents in iOS Files app by enabling the configuration in your project. +- Handled the punctuation(\n, \t, \t, and ,) characters in CSV file. ## iOS/MacOS import headers @@ -129,18 +130,20 @@ print(filePath) let user1:NSMutableDictionary = NSMutableDictionary() user1.setObject("vignesh", forKey: "name" as NSCopying); user1.setObject("vigneshuvi@gmail.com", forKey: "email" as NSCopying); +user1.setObject("Hi Vignesh, \nhow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "address" as NSCopying); // Secound User Object let user2:NSMutableDictionary = NSMutableDictionary() user2.setObject("vinoth", forKey: "name" as NSCopying); user2.setObject("vinoth@gmail.com", forKey: "email" as NSCopying); +user2.setObject("Hi Vinoth, \nHow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "address" as NSCopying); // Add dictionary into rows of CSV Array let data:NSMutableArray = NSMutableArray() data.add(user1); data.add(user2); -let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email"],values: data); +let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email", "address"],values: data); print(filePath) ``` diff --git a/SwiftCSVExport.podspec b/SwiftCSVExport.podspec index 5733bd2..43d43b0 100644 --- a/SwiftCSVExport.podspec +++ b/SwiftCSVExport.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "SwiftCSVExport" - s.version = "1.0.4" + s.version = "1.0.5" s.summary = "Simple way to export csv file with rich feature framework and written in Swift 3." # This description is used to generate tags and improve search results. diff --git a/SwiftCSVExport/Sources/CSVExport.swift b/SwiftCSVExport/Sources/CSVExport.swift index 60d2e4f..c85a491 100644 --- a/SwiftCSVExport/Sources/CSVExport.swift +++ b/SwiftCSVExport/Sources/CSVExport.swift @@ -76,13 +76,23 @@ extension String { var result = "" for key in fields { if let value = dictionary.object(forKey: key) { - if result.characters.count == 0 { - result = "\(value)" + if let string = value as? String { + // obj is a String. Do something with str + if result.characters.count == 0 { + result = "\"\(string)\"" + } else { + result = "\(result),\"\(string)\"" + } } else { - result = "\(result),\(value)" + if result.characters.count == 0 { + result = "\(value)" + } else { + result = "\(result),\(value)" + } } } else { + if result.characters.count == 0 { result = "\("")" } else { @@ -123,7 +133,7 @@ extension String { open func write(text: String) { let path = "\(directory)/\(self.csvFileName())" let fileManager = FileManager.default - let updatedString = text.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\t", with: "").replacingOccurrences(of: "\r", with: "") + let updatedString = text.replacingOccurrences(of: "\n", with: "\\n").replacingOccurrences(of: "\t", with: "\\t").replacingOccurrences(of: "\r", with: "\\r") if !fileManager.fileExists(atPath: path) {