Skip to content

Commit

Permalink
- Bug fixes and code refactor .
Browse files Browse the repository at this point in the history
- Declared public NSMutableDictionary extension to fetch CSV file details.
- Updated the example code.
  • Loading branch information
vigneshuvi committed Jan 2, 2018
1 parent 0e2528b commit 0b69457
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 304 deletions.
4 changes: 3 additions & 1 deletion Examples/SampleSwift/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ target 'SampleSwift' do
use_frameworks!

pod 'SwiftLoggly’
pod 'SwiftCSVExport', :git => 'https://github.com/vigneshuvi/SwiftCSVExport.git', :tag => '1.0.4'
pod 'SwiftCSVExport'
# pod 'SwiftCSVExport', :git => 'https://github.com/vigneshuvi/SwiftCSVExport.git', :tag => '1.0.6'
# Pods for SampleSwift
Expand Down
47 changes: 34 additions & 13 deletions Examples/SampleSwift/SampleSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,61 @@
//

import UIKit
import Foundation
import SwiftLoggly
import SwiftCSVExport

import UIKit
import SwiftLoggly
import SwiftCSVExport

class User {
var userid:Int = 0
var name:String = ""
var email:String = ""
var isValidUser:Bool = false
var message:String = ""
var balance:Double = 0.0
}


class ViewController: UIViewController {



@IBOutlet var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()


// Generate CSV file
let user1:NSMutableDictionary = NSMutableDictionary()
user1.setObject(107, forKey: "userid" as NSCopying);
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);
user1.setObject(true, forKey:"isValidUser" as NSCopying)
user1.setObject("Hi 'Vignesh!' \nhow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "message" as NSCopying);
user1.setObject(571.05, forKey: "balance" as NSCopying);

let user2:NSMutableDictionary = NSMutableDictionary()
user2.setObject(108, forKey: "userid" as NSCopying);
user2.setObject("vinoth", forKey: "name" as NSCopying);
user2.setObject(false, forKey:"isValidUser" 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);

user2.setObject("Hi 'Vinoth!', \nHow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "message" as NSCopying);
user2.setObject(567.50, forKey: "balance" as NSCopying);

let data:NSMutableArray = NSMutableArray()
data.add(user1);
data.add(user2);

let user3 = User()
user3.userid = 109
user3.name = "John"
user3.email = "John@gmail.com"
user3.isValidUser = true
user3.message = "Hi 'John!' \nHow are you? \t Shall we meet tomorrow? \r Thanks "
user3.balance = 105.41;
data.add(listPropertiesWithValues(user3)) // Able to convert Class object into NSMutableDictionary

let header = ["userid", "name", "email", "message", "isValidUser","balance"]
// Create a object for write CSV
let writeCSVObj = CSV()
writeCSVObj.rows = data
writeCSVObj.fields = ["name", "email", "address"]
writeCSVObj.fields = header as NSArray
writeCSVObj.name = "userlist"

// Write File using CSV class object
Expand All @@ -57,13 +74,17 @@ class ViewController: UIViewController {

//
let fileDetails = readCSV(filePath);
if fileDetails.allKeys.count > 0 {
if fileDetails.hasData {
loggly(LogType.Info, dictionary: fileDetails)
loggly(LogType.Info, text: fileDetails.name)
loggly(LogType.Info, text: fileDetails.delimiter)
}

// Read File in Object Oriented Way
let readCSVObj = readCSVObject(filePath);



// Use 'SwiftLoggly' pod framework to print the Dictionary
loggly(LogType.Info, text: readCSVObj.name)

Expand Down
174 changes: 72 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Simple way to export csv file with rich feature framework and written in Swift.

## Features

- Able to give CSV file name.
- Able to set CSV headers using fields.
- Able to give CSV file name, headers and rows using property keys.
- Able to convert JSON string into CSV.
- Able to Read the CSV file and convert to NSDictionary.
- Able to Read the CSV file and convert to CSV class(Object Oriented Approach).
Expand Down Expand Up @@ -94,84 +93,67 @@ NSLog(@"%@",filePath);

```

### Example 2 - Swift
### Example 2 - Swift - Object Oriented Approach

```swift

// First User Object
let user1:NSMutableDictionary = NSMutableDictionary()
user1.setObject(107, forKey: "userid" as NSCopying);
user1.setObject("vignesh", forKey: "name" as NSCopying);
user1.setObject("vigneshuvi@gmail.com", forKey: "email" as NSCopying);
user1.setObject(true, forKey:"isValidUser" as NSCopying)
user1.setObject("Hi 'Vignesh!' \nhow are you? \t Shall we meet tomorrow? \r Thanks ", forKey: "message" as NSCopying);
user1.setObject(571.05, forKey: "balance" as NSCopying);

// Secound User Object
let user2:NSMutableDictionary = NSMutableDictionary()
user2.setObject(108, forKey: "userid" as NSCopying);
user2.setObject("vinoth", forKey: "name" as NSCopying);
user2.setObject(false, forKey:"isValidUser" 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: "message" as NSCopying);
user2.setObject(567.50, forKey: "balance" as NSCopying);

// Add fields into columns of CSV headers
let fields:NSMutableArray = NSMutableArray()
fields.add("name");
fields.add("email");
let header = ["userid", "name", "email", "message", "isValidUser","balance"]


// Add dictionary into rows of CSV Array
let data:NSMutableArray = NSMutableArray()
data.add(user1);
data.add(user2);

let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: fields,values: data);
print(filePath)

```

### Example 3 - Swift
```swift


// First User Object
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);
// Create a object for write CSV
let writeCSVObj = CSV()
writeCSVObj.rows = data
writeCSVObj.fields = header as NSArray
writeCSVObj.name = "userlist"

// Add dictionary into rows of CSV Array
let data:NSMutableArray = NSMutableArray()
data.add(user1);
data.add(user2);
// Write File using CSV class object
let filePath:String = SwiftCSVExport.exportCSV(writeCSVObj);
print("File Path: \(filePath)")

let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email", "address"],values: data);
print(filePath)
// Read File and convert as CSV class object
let readCSVObj = readCSVObject(filePath);

// Use 'SwiftLoggly' pod framework to print the Dictionary
loggly(LogType.Info, text: readCSVObj.name)

```

### Example 4 - Swift
```swift

### Write Output:

// Generate CSV file
let user1:NSMutableDictionary = NSMutableDictionary()
user1.setObject("vignesh", forKey: "name" as NSCopying);
user1.setObject("vigneshuvi@gmail.com", forKey: "email" as NSCopying);
```swift

let user2:NSMutableDictionary = NSMutableDictionary()
user2.setObject("vinoth", forKey: "name" as NSCopying);
user2.setObject("vinoth@gmail.com", forKey: "email" as NSCopying);
File Path: xxxxxx/xxxxxxx/Documents/Exports/userlist.csv

let data:NSMutableArray = NSMutableArray()
data.add(user1);
data.add(user2);
[💙 Info - Jan 2, 2018, 4:52:28 PM]: userlist.csv

let filePath:String = SwiftCSVExport.exportCSV("userlist",fields: ["name", "email"],values: data);
print(filePath)

```

### Example 5 - Swift
### Example 3 - Swift

```swift

Expand All @@ -183,9 +165,14 @@ print(filePath)
// Read File
let fileDetails = readCSV(filePath);

// Read File and convert as NSMutableDictionary.
let fileDetails = readCSV(filePath);

// Use 'SwiftLoggly' pod framework to print the Dictionary
if fileDetails.allKeys.count > 0 {
if fileDetails.hasData {
loggly(LogType.Info, dictionary: fileDetails)
loggly(LogType.Info, text: fileDetails.name)
loggly(LogType.Info, text: fileDetails.delimiter)
}


Expand All @@ -197,13 +184,14 @@ if fileDetails.allKeys.count > 0 {

Output: userlist.csv

name,email
vignesh,vigneshuvi@gmail.com
vinoth,vinoth@gmail.com
userid,name,email,message,isValidUser,balance
107, "vignesh", "vigneshuvi@gmail.com", "Hi 'Vignesh!' \nhow are you? \t Shall we meet tomorrow? \r Thanks ", 1, 571.05
108, "vinoth", "vinoth@gmail.com", "Hi 'Vinoth!', \nHow are you? \t Shall we meet tomorrow? \r Thanks ", 0, 567.5
109, "John", "John@gmail.com", "Hi 'John!' \nHow are you? \t Shall we meet tomorrow? \r Thanks ", 1, 105.41

```

### Example 6 - Swift
### Example 4 - Swift

```swift

Expand All @@ -222,71 +210,53 @@ if fileDetails.allKeys.count > 0 {

```swift

[💙 Info - Feb 7, 2017, 4:19:23 PM]: {
[💙 Info - Jan 2, 2018, 4:52:21 PM]: {
"fields" : [
"userid",
"name",
"email",
"message",
"isValidUser",
"balance"
],
"rows" : [
{
"name" : "vignesh",
"email" : "vigneshuvi@gmail.com"
"email" : "\"vigneshuvi@gmail.com\"",
"message" : "\"Hi 'Vignesh!' \\nhow are you? \\t Shall we meet tomorrow? \\r Thanks \"",
"userid" : 107,
"name" : "\"vignesh\"",
"isValidUser" : 1,
"balance" : 571.05
},
{
"email" : "\"vinoth@gmail.com\"",
"message" : "\"Hi 'Vinoth!', \\nHow are you? \\t Shall we meet tomorrow? \\r Thanks \"",
"userid" : 108,
"name" : "\"vinoth\"",
"isValidUser" : 0,
"balance" : 567.5
},
{
"name" : "vinoth",
"email" : "vinoth@gmail.com"
"email" : "\"John@gmail.com\"",
"message" : "\"Hi 'John!' \\nHow are you? \\t Shall we meet tomorrow? \\r Thanks \"",
"userid" : 109,
"name" : "\"John\"",
"isValidUser" : 1,
"balance" : 105.41
}
],
"name" : "userlist.csv",
"fields" : [
"name",
"email"
]
"divider" : ","
}


```

### Example 7 - Swift - Object Oriented Approach

```swift

// Generate CSV file
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);

// Create a object for write CSV
let writeCSVObj = CSV()
writeCSVObj.rows = data
writeCSVObj.fields = ["name", "email", "address"]
writeCSVObj.name = "userlist"

// Write File using CSV class object
let filePath:String = SwiftCSVExport.exportCSV(writeCSVObj);
print(filePath)

// Read File in Object Oriented Way
let readCSVObj = readCSVObject(filePath);

// Use 'SwiftLoggly' pod framework to print the Dictionary
loggly(LogType.Info, text: readCSVObj.name)


```

### Write Output:

```swift

Output: userlist
Output: userlist.csv

```

Expand Down
8 changes: 6 additions & 2 deletions 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.6"
s.version = "1.0.7"
s.summary = "Simple way to export csv file with rich feature framework and written in Swift 3 & 4."

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -69,7 +69,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.9"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
s.tvos.deployment_target = "9.0"


# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand All @@ -93,6 +93,10 @@ Pod::Spec.new do |s|
# s.exclude_files = "Classes/Exclude"

# s.public_header_files = "Classes/**/*.h"
s.test_spec 'SwiftCSVExportTests' do |test_spec|
test_spec.source_files = 'SwiftCSVExportTests//**/*.{h,m,swift}'
test_spec.dependency 'OCMock'
end


# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
Loading

0 comments on commit 0b69457

Please sign in to comment.