Csv Swift Reference Documentation
CkoCsv
Current Version: 11.5.0
Chilkat.Csv
Load CSV data, modify cells, add rows, delete rows or columns, and write
the updated table back to a file or string.
Use a header row to access fields by column name instead of only by
numeric column index.
Read and update individual cells, insert columns, remove data, find
matching rows, and sort the table as needed.
Work with comma-separated data or other delimiter-separated formats by
configuring the delimiter character.
Control quoted fields, forced quoting, output line endings, explicit
character encodings, and UTF-8 BOM emission.
Load worksheet data from an opened
For an extended overview, see
Csv Class Overview.
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
Header and named columns
Cell and row operations
Delimiter control
Quoting and output formatting
Excel worksheet loading
.xlsx ZIP container when
CSV-style access to spreadsheet content is needed.
Object Creation
let obj = CkoCsv()!
Properties
AutoTrim
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.
Crlf
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.
DebugLogFilePath
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.
Delimiter
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.
EnableQuotes
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.
EscapeBackslash
Controls whether the backslash character (\) is treated as an
escape character when reading or writing CSV data.
The default value is false.
HasColumnNames
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.
LastErrorHtml
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.
topLastErrorText
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.
LastErrorXml
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.
topLastMethodSuccess
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.
NumColumns
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.
NumRows
The number of data rows in the CSV.
-
When
HasColumnNamesistrue, the column-name row is not included in the count. - Whitespace-only lines following the last non-empty row are not included.
UncommonOptions
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.
VerboseLogging
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.
Version
XlsxDateFormat
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.
Methods
DeleteColumn
Deletes the column at zero-based index . The first column is at index
0.
Returns true for success, false for failure.
DeleteColumnByName
Deletes the column whose name is specified by byName.
Returns true for success, false for failure.
DeleteRow
Deletes the entire row at zero-based index . The first data row is at
index 0.
Returns true for success, false for failure.
GetCell
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
GetCellByName
Returns the contents of the cell in row byName and the column named by columnName.
Row indexing begins at 0.
Returns nil on failure
GetColumnName
Returns the name of the column at zero-based index . The first column is
at index 0.
Returns nil on failure
GetIndex
Returns the zero-based index of the column named by .
Returns -1 if no column has the specified name.
GetNumCols
Returns the number of columns in the row at zero-based index .
Returns 0 if is beyond the available rows.
InsertColumn
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.
LoadFile
Loads CSV data from the file specified by .
The file is interpreted using the ANSI character encoding.
Returns true for success, false for failure.
topLoadFile2
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.
LoadFromString
Loads CSV data from the in-memory string contained in fromString.
Returns true for success, false for failure.
RowMatches
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
caseSensitiveistrue, matching is case-sensitive. WhencaseSensitiveisfalse, matching is case-insensitive.
Returns true when the row matches the pattern; otherwise returns false.
SaveFile
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.
topSaveFile2
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.
SaveToSb
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.
topSaveToString
Returns the entire CSV document as a string.
The record separator is controlled by the Crlf property.
Returns nil on failure
SetCell
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.
SetCellByName
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.
SetColumnName
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.
SortByColumn
Sorts the CSV rows using the values in the column named by byColumn.
- When
ascendingistrue, rows are sorted in ascending order. - When
ascendingisfalse, rows are sorted in descending order. - When
caseSensitiveistrue, comparisons are case-sensitive. - When
caseSensitiveisfalse, comparisons are case-insensitive.
Returns true for success, false for failure.
topSortByColumnIndex
Sorts the CSV rows using the values in the column at zero-based index byColumnIndex.
- When
ascendingistrue, rows are sorted in ascending order. - When
ascendingisfalse, rows are sorted in descending order. - When
caseSensitiveistrue, comparisons are case-sensitive. - When
caseSensitiveisfalse, comparisons are case-insensitive.
Returns true for success, false for failure.
topXlsxGetSheets
Gets the names of the worksheets contained in an Excel .xlsx
spreadsheet.
-
is aZipobject containing the already-opened.xlsxfile. sheetNamesreceives the worksheet names.
Returns true for success, false for failure.
XlsxLoadSheet
Loads a worksheet from an Excel .xlsx spreadsheet into this CSV
object.
-
is aZipobject containing the already-opened.xlsxfile. sheetNameis 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.