Skip to content

Commit

Permalink
- Updated README file.
Browse files Browse the repository at this point in the history
- Handled the punctuation(\n, \t, \t, and ,) characters in CSV file.
- Updated the SampleSwift with Examples.
  • Loading branch information
vigneshuvi committed Nov 7, 2017
1 parent d1f1923 commit 8ac0ec6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Examples/SampleSwift/SampleSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

```
Expand Down
2 changes: 1 addition & 1 deletion SwiftCSVExport.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions SwiftCSVExport/Sources/CSVExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8ac0ec6

Please sign in to comment.