Csv Swift Reference Documentation

CkoCsv

Current Version: 11.5.0

Chilkat.Csv

Read, edit, sort, and write CSV or delimiter-separated data in memory.

Chilkat.Csv is a practical in-memory table class for working with CSV and other delimiter-separated text formats. It can load CSV data from files or strings, access cells by row/column index or column name, manage header rows, insert and delete rows or columns, sort records, search for matching rows, and write the result back using configurable delimiters, quoting rules, character encodings, and line endings.

In-memory CSV table

Load CSV data, modify cells, add rows, delete rows or columns, and write the updated table back to a file or string.

Header and named columns

Use a header row to access fields by column name instead of only by numeric column index.

Cell and row operations

Read and update individual cells, insert columns, remove data, find matching rows, and sort the table as needed.

Delimiter control

Work with comma-separated data or other delimiter-separated formats by configuring the delimiter character.

Quoting and output formatting

Control quoted fields, forced quoting, output line endings, explicit character encodings, and UTF-8 BOM emission.

Excel worksheet loading

Load worksheet data from an opened .xlsx ZIP container when CSV-style access to spreadsheet content is needed.

Common pattern: Load CSV data, specify whether the first row is a header, access or modify cells by index or column name, perform any row, column, search, or sort operations, then save the table using the delimiter, charset, quoting, BOM, and line-ending settings required by the target system.

Object Creation

let obj = CkoCsv()!

Properties

AutoTrim
autoTrim: Bool

Controls whether leading and trailing whitespace is removed from cell values returned by GetCell and GetCellByName.

  • When true, returned cell values are automatically trimmed.
  • When false, returned cell values are left unchanged.

top
Crlf
crlf: Bool

Controls the line endings used when the CSV is written by SaveFile, SaveFile2, or SaveToString.

  • When true, records are separated by CRLF line endings.
  • When false, records are separated by LF line endings.

top
DebugLogFilePath
debugLogFilePath: String!

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
Delimiter
delimiter: String!

Specifies the character used to separate fields within each CSV record. The default delimiter is a comma (,).

When loading CSV data, Chilkat automatically detects the delimiter if this property was not explicitly set. A semicolon (;) is commonly used in locales where a comma is used as the decimal separator.

Creating a new CSV: To use a delimiter other than a comma, set this property before adding rows or columns.

Vertical-bar delimiter: If this property is explicitly set to |, and EnableQuotes has not been explicitly set, EnableQuotes defaults to false. This allows quotation marks to be treated as ordinary characters in vertical-bar-delimited data.

top
EnableQuotes
enableQuotes: Bool
Introduced in version 9.5.0.71

Controls whether double quotation marks are interpreted as CSV field delimiters when reading or writing data.

When true, content enclosed in double quotation marks is treated as a single field, even when it contains delimiter characters, carriage returns, or line feeds. The default value is true.

More Information and Examples
top
EscapeBackslash
escapeBackslash: Bool
Introduced in version 9.5.0.44

Controls whether the backslash character (\) is treated as an escape character when reading or writing CSV data.

The default value is false.

top
HasColumnNames
hasColumnNames: Bool

Indicates whether the first record contains column names rather than data.

Set this property to true before loading CSV data when the first record is a header row. Chilkat will then load the field names as column names and exclude the header row from the data-row count.

More Information and Examples
top
LastErrorHtml
lastErrorHtml: String! (read-only)

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
lastErrorText: String! (read-only)

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
lastErrorXml: String! (read-only)

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
lastMethodSuccess: Bool

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
NumColumns
numColumns: Int (read-only)

The number of columns in the first row of the CSV.

When HasColumnNames is true, the first row is the column-name row, and this property reports the number of column names.

top
NumRows
numRows: Int (read-only)

The number of data rows in the CSV.

  • When HasColumnNames is true, the column-name row is not included in the count.
  • Whitespace-only lines following the last non-empty row are not included.

top
UncommonOptions
uncommonOptions: String!
Introduced in version 9.5.0.88

Provides optional keywords for uncommon CSV-processing requirements. This property should normally remain empty. To enable more than one option, use a comma-separated list of keywords.

The following keywords are defined:

  • QuotedCells (v9.5.0.96) — Encloses every cell value in double quotation marks when writing the CSV to a file or string.
  • QuotedColumnNames (v9.5.0.96) — Encloses every column name in double quotation marks when writing the CSV to a file or string.
  • EMIT_BOM (v9.5.0.93) — Writes a UTF-8 byte order mark (BOM) when saving the CSV to a UTF-8 encoded file.

top
VerboseLogging
verboseLogging: Bool

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
version: String! (read-only)

Version of the component/library, such as "10.1.0"

More Information and Examples
top
XlsxDateFormat
xlsxDateFormat: String!
Introduced in version 11.3.0

Specifies the date format used when converting date cells from an Excel .xlsx worksheet to CSV text.

The default format is mm-dd-yy. Other valid formats include d/m/yyyy, m/d/yyyy, and similar date-format patterns.

top

Methods

DeleteColumn
deleteColumn(index: Int) -> Bool

Deletes the column at zero-based index . The first column is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
DeleteColumnByName
deleteColumn(byName: String) -> Bool

Deletes the column whose name is specified by byName.

Returns true for success, false for failure.

More Information and Examples
top
DeleteRow
deleteRow(index: Int) -> Bool

Deletes the entire row at zero-based index . The first data row is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
GetCell
getCell(row: Int, col: Int) -> String

Returns the contents of the cell at row and column col.

Row and column indexes are zero-based. The upper-left data cell is at row 0, column 0.

Returns nil on failure

More Information and Examples
top
GetCellByName
getCell(byName: Int, columnName: String) -> String

Returns the contents of the cell in row byName and the column named by columnName.

Row indexing begins at 0.

Returns nil on failure

top
GetColumnName
getColumnName(index: Int) -> String

Returns the name of the column at zero-based index . The first column is at index 0.

Returns nil on failure

More Information and Examples
top
GetIndex
getIndex(columnName: String) -> Int

Returns the zero-based index of the column named by .

Returns -1 if no column has the specified name.

top
GetNumCols
getNumCols(row: Int) -> Int

Returns the number of columns in the row at zero-based index .

Returns 0 if is beyond the available rows.

top
InsertColumn
insertColumn(index: Int) -> Bool
Introduced in version 9.5.0.89

Inserts a new empty column immediately before the column at zero-based index . The first column is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
LoadFile
loadFile(path: String) -> Bool

Loads CSV data from the file specified by .

The file is interpreted using the ANSI character encoding.

Returns true for success, false for failure.

top
LoadFile2
loadFile2(path: String, charset: String) -> Bool

Loads CSV data from the file specified by . charset specifies the character encoding used by the file.

See Supported Charsets for the character-encoding names recognized by Chilkat.

Returns true for success, false for failure.

More Information and Examples
top
LoadFromString
load(fromString: String) -> Bool

Loads CSV data from the in-memory string contained in fromString.

Returns true for success, false for failure.

More Information and Examples
top
RowMatches
rowMatches(row: Int, matchPattern: String, bCaseSensitive: Bool) -> Bool

Compares the contents of the row at zero-based index with the wildcard pattern in matchPattern.

  • The asterisk character (*) may appear any number of times and matches zero or more characters.
  • When caseSensitive is true, matching is case-sensitive. When caseSensitive is false, matching is case-insensitive.

Returns true when the row matches the pattern; otherwise returns false.

top
SaveFile
saveFile(path: String) -> Bool

Writes the CSV to the file specified by .

The output file is written using the ANSI character encoding.

Returns true for success, false for failure.

top
SaveFile2
saveFile2(path: String, charset: String) -> Bool

Writes the CSV to the file specified by . charset specifies the character encoding to use for the output file. Chilkat converts the CSV text to that encoding while saving.

See Supported Charsets for the character-encoding names recognized by Chilkat.

Returns true for success, false for failure.

More Information and Examples
top
SaveToSb
save(toSb: CkoStringBuilder) -> Bool
Introduced in version 9.5.0.93

Clears toSb and writes the entire CSV document into it.

The record separator is controlled by the Crlf property.

Returns true for success, false for failure.

top
SaveToString
saveToString() -> String

Returns the entire CSV document as a string.

The record separator is controlled by the Crlf property.

Returns nil on failure

top
SetCell
setCell(row: Int, col: Int, content: String) -> Bool

Sets the cell at row and column col to the string in content. Row and column indexes are zero-based.

content may contain any characters, including CRLF line endings, double quotation marks, and delimiter characters. When the CSV is saved, Chilkat automatically quotes fields that require quoting. When CSV data is loaded, quoted and escaped fields are parsed automatically.

Returns true for success, false for failure.

top
SetCellByName
setCell(byName: Int, columnName: String, content: String) -> Bool

Sets the cell in row byName and the column named by columnName to the string in contentStr. Row indexing begins at 0.

contentStr may contain delimiter characters, quotation marks, and line endings. Chilkat applies the required CSV quoting when the data is saved.

Returns true for success, false for failure.

More Information and Examples
top
SetColumnName
setColumnName(index: Int, columnName: String) -> Bool

Sets the name of the column at zero-based index to columnName. The first column is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
SortByColumn
sort(byColumn: String, bAscending: Bool, bCaseSensitive: Bool) -> Bool

Sorts the CSV rows using the values in the column named by byColumn.

  • When ascending is true, rows are sorted in ascending order.
  • When ascending is false, rows are sorted in descending order.
  • When caseSensitive is true, comparisons are case-sensitive.
  • When caseSensitive is false, comparisons are case-insensitive.

Returns true for success, false for failure.

top
SortByColumnIndex
sort(byColumnIndex: Int, bAscending: Bool, bCaseSensitive: Bool) -> Bool
Introduced in version 9.5.0.83

Sorts the CSV rows using the values in the column at zero-based index byColumnIndex.

  • When ascending is true, rows are sorted in ascending order.
  • When ascending is false, rows are sorted in descending order.
  • When caseSensitive is true, comparisons are case-sensitive.
  • When caseSensitive is false, comparisons are case-insensitive.

Returns true for success, false for failure.

top
XlsxGetSheets
xlsxGetSheets(xlsx: CkoZip, sheetNames: CkoStringTable) -> Bool
Introduced in version 11.3.0

Gets the names of the worksheets contained in an Excel .xlsx spreadsheet.

  • is a Zip object containing the already-opened .xlsx file.
  • sheetNames receives the worksheet names.

Returns true for success, false for failure.

More Information and Examples
top
XlsxLoadSheet
xlsxLoadSheet(zip: CkoZip, sheetName: String) -> Bool
Introduced in version 11.3.0

Loads a worksheet from an Excel .xlsx spreadsheet into this CSV object.

  • is a Zip object containing the already-opened .xlsx file.
  • sheetName is the name of the worksheet to load.
  • To load the first, or default, worksheet, pass an empty string in sheetName.

Returns true for success, false for failure.

top