Latest 1.0.3
Homepage https://github.com/russell-davis/LDCSVParser
License MIT Copyright 2018 Russell Luke Davis
Free use of the entirety of this project with or without the explicit permission of the author
Platforms ios 11.4
Authors

CSVParser

Build Status
Version
MIT Licence


Installation

Clone the repo, open the project, and run. After successful run, navigate to the ‘Products’ folder and click and drag the framework to your projects directory.

import LDCSVParser

The Parser

How it works

The way the parser works, is by first parsing out each line, and creating a key value pair (ultimately an array of dictionaries) and returning a completed set, known as the keyedRows.

After creating the keyed rows, the parser iterates through the keyedRows and removes encoded values like ‘n’, ‘&nbsp’, etc.

Finally, the parser builds your custom object by using the JSONDecoder to effectivly map values to your objects properties.

Create an instance of the parser using a string

let csv = "id,name,agen1,Alice,18n2,Bob,19n3,Charlie,20"
let parser = CSVParser(with: csv)

Create an instance of the parser using a string, and custom headers

let csv = "id,name,agen1,Alice,18n2,Bob,19n3,Charlie,20"
parser = CSVParser(with: csv, headers: headers)

Create an instance of the parser using a string, a custom separator, and custom headers

let csv = "id,name,agen1,Alice,18n2,Bob,19n3,Charlie,20"
parser = CSVParser(with: csv, separator: separator, headers: header)

Useful Properties

parser.headers // The headers from the data, an Array of String
parser.keyedRows // An array of Dictionaries with the values of each row keyed to the header
parser.rows // An Array of Arrays of the values

Object Creation

Creating objects from the csv data is easy. The parser has the method to take the keyedrows and create individual objects from them.

NOTE: the .build method is is a generic, and as such, the completion handler must be provided a class that conforms to Codeable.

Consider the Following class:


// Class must conform to Codable
// Properties must be defined as optional
class Person: Codable {
    let name: String?
    let age: String?
    let id: String?

// Use the CodingKeys enum to set the actual name of the CSV header that you are trying to map
enum CodingKeys: String, CodingKey {
        case name = "First and Last Name"
        case age
        case id = "ID"
    }
}

A Note On CodingKeys:

For example, if our CSV headers are "id,name,age" AND our properties are name the exact same, we don’t really need to provide the CodingKeys; however, if our headers are "ID,First and Last Name,age", you can see the parser would miss these values and as such, we need to tell our class that the values we are looking for are called something different.

class SomeViewController {
  func someMethond() {

    if let keyed = parser.keyedRows {

      // build an object with the keyed rows using the Person class
      parser.build(with: keyed) { (people: [Person]) in
        for person in people {
          print(person.name)
        }
      }
    }

  }
}

Latest podspec

{
    "name": "LDCSVParser",
    "version": "1.0.3",
    "summary": "LDCSVParser helps turn raw CSV into useable objects",
    "description": "LDCSVParser takes a raw CSV string and maps headers and values to keyvalue pairs, then has the ability to create custom objects using generics in Swift",
    "homepage": "https://github.com/russell-davis/LDCSVParser",
    "license": {
        "type": "MIT",
        "text": "                            Copyright 2018 Russell Luke Davisn                            Free use of the entirety of this project with or without the explicit permission of the authorn"
    },
    "authors": {
        "Luke Davis": "[email protected]"
    },
    "platforms": {
        "ios": "11.4"
    },
    "source": {
        "git": "https://github.com/russell-davis/LDCSVParser.git",
        "tag": "1.0.3"
    },
    "source_files": "LDCSVParser"
}

Pin It on Pinterest

Share This