Skip to content

Commit

Permalink
Able to encoding CSV based on String.Encoding Type(utf8, ascii, unico…
Browse files Browse the repository at this point in the history
…de, utf16, etc) Refer: String.Encoding.
  • Loading branch information
vigneshuvi committed Mar 20, 2017
1 parent 284dfc2 commit 18e2acc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Simple way to export csv file with rich feature framework in Swift.
- Able to convert JSON string into CSV.
- Able to Read the CSV file and convert to NSDictionary.
- 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.

##iOS/MacOS import headers

Expand Down Expand Up @@ -225,7 +226,7 @@ Then run:

carthage update

###Swift Package Manager for Vapor
### Swift Package Manager for Vapor

You need to add to dependencies in your 'Package.swift' and fetch Swift module using terminal comment.

Expand Down
11 changes: 7 additions & 4 deletions Sources/CSVExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ open class CSVExport {
//The name of the csv files.
open var fileName = "csvfile";

//The CSV encodeing format
open var encodingType:String.Encoding = String.Encoding.utf8;


///export singleton
open class var export: CSVExport {
Expand Down Expand Up @@ -70,14 +73,14 @@ open class CSVExport {
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: path) {
do {
try "".write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
try "".write(toFile: path, atomically: true, encoding: encodingType)
} catch _ {
}
}
if let fileHandle = FileHandle(forWritingAtPath: path) {
let writeText = "\(text)\n"
fileHandle.seekToEndOfFile()
fileHandle.write(writeText.data(using: String.Encoding.utf8)!)
fileHandle.write(writeText.data(using: encodingType)!)
fileHandle.closeFile()
print(writeText, terminator: "")

Expand Down Expand Up @@ -122,7 +125,7 @@ open class CSVExport {
let localPathURL: URL = NSURL.fileURL(withPath: filePath);

// Read the content from Local Path
let csvText = try String(contentsOf: localPathURL, encoding: String.Encoding.utf8);
let csvText = try String(contentsOf: localPathURL, encoding: encodingType);

// Check the csv count
if csvText.characters.count > 0 {
Expand Down Expand Up @@ -238,7 +241,7 @@ public func exportCSV(_ filename:String, fields: [String], values: NSArray) -> S
public func exportCSV(_ filename:String, fields: [String], values: String) -> String{

// Convert String into NSArray of objects.
if let data = (values as NSString).data(using: String.Encoding.utf8.rawValue)
if let data = (values as NSString).data(using: CSVExport.export.encodingType.rawValue)
{
do {
let parsedObject = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableLeaves) as! NSArray
Expand Down

0 comments on commit 18e2acc

Please sign in to comment.