Swift CSV Export

Simple way to export csv file with rich feature framework in Swift.

Posted by Vigneshuvi on January 18, 2017

Features

Framework with major highlights

  • Able to give CSV file name.
  • Able to set CSV headers using fields.
  • 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).

iOS/MacOS import headers

First thing is to import the framework. See the Installation instructions on how to add the framework to your project.

// iOS
import SwiftCSVExport


// macOS
import SwiftCSVExportOSX

Examples


Example 1 - Able to convert NSMutableDictionary into CSV

// First User Object
let user1:NSMutableDictionary = NSMutableDictionary()
user1.setObject("vignesh", forKey: "name" as NSCopying);
user1.setObject("vigneshuvi@gmail.com", forKey: "email" as NSCopying);

// Secound User Object
let user1:NSMutableDictionary = NSMutableDictionary()
user1.setObject("vinoth", forKey: "name" as NSCopying);
user1.setObject("vinoth@gmail.com", forKey: "email" as NSCopying);

// Add fields into columns of CSV headers
let fields:NSMutableArray = NSMutableArray()
fields.add("name");
fields.add("email");

// 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)

Write Output

Output: userlist.csv

name,email
vignesh,vigneshuvi@gmail.com
vinoth,vinoth@gmail.com


Example 2 - Able to convert JSON string into CSV.

// Able to convert JSON string into CSV.
let string = "[{\"name\":\"vignesh\",\"email\":\"vigneshuvi@gmail.com\"},{\"name\":\"vinoth\",\"email\":\"vinoth@gmail.com\"}]";
let filePath:String = exportCSV("userlist", fields:["name","email"], values:string);
print(filePath)

// Read File
let fileDetails = readCSV(filePath);
// Use 'SwiftLoggly' pod framework to print the Dictionary/span>
if fileDetails.allKeys.count > 0 {
loggly(LogType.Info, dictionary: fileDetails)
}

Read Output

[💙 Info - Feb 7, 2017, 4:19:23 PM]: {
"rows" : [ { "name" : "vignesh", "email" : "vigneshuvi@gmail.com" },
{"name" : "vinoth", "email" : "vinoth@gmail.com" } ],
"name" : "userlist.csv",
"fields" : ["name", "email"] }

That will create a CSV file in the proper directory on both OS X and iOS.

OS X CSV files will be created in the OS X Exports directory (found under: /Library/Exports). The iOS CSV files will be created in your apps document directory under a folder called Exports.

Configuration


//Set the name of the csv file
CSVExport.export.fileName = "Sample" //default is "csvfile"

//Set the directory in which the csv files will be written
CSVExport.export.directory = "/Library/XXX-folder-name-XXX" //default is the standard exporting directory for each platform.

Installation

CocoaPods - To use SwiftCSVExport in your project add the following 'Podfile' to your project

platform :ios, '8.0'
use_frameworks!
pod 'SwiftCSVExport'

Then run:
pod install || pod update

Swift Package Manager for Vapor

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

// Vapor
dependencies: [ .Package(url: "https://github.com/vigneshuvi/SwiftCSVExport.git", majorVersion: 1, minor: 0) ]

Then run:
vapor build || vapor xcode

// Importing header
import SwiftCSVExport

License

SwiftCSVExport is licensed under the MIT License.

Source available here!! SwiftCSVExport