PrivMX DOCS
API Reference/PrivMX Endpoint Swift Extra/Extensions

String

A Unicode string value that is a collection of characters. A string is a series of characters, such as "Swift", that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient. The String type bridges with the Objective-C class NSString and offers interoperability with C functions that works with strings. You can create new strings using string literals or string interpolations. A is a series of characters enclosed in quotes.

are string literals that evaluate any included expressions and convert the results to string form. String interpolations give you an easy way to build a string from multiple pieces. Wrap each expression in a string interpolation in parentheses, prefixed by a backslash.

Combine strings using the concatenation operator (+).

Multiline string literals are enclosed in three double quotation marks ("""), with each delimiter on its own line. Indentation is stripped from each line of a multiline string literal to match the indentation of the closing delimiter.

Strings always have value semantics. Modifying a copy of a string leaves the original unaffected.

Comparing strings for equality using the equal-to operator (==) or a relational operator (like \< or \>=) is always performed using Unicode canonical representation. As a result, different representations of a string compare as being equal.

The Unicode scalar value "\u\{301\}" modifies the preceding character to include an accent, so "e\u\{301\}" has the same canonical representation as the single Unicode scalar value "é". Basic string operations are not sensitive to locale settings, ensuring that string comparisons and other operations always have a single, stable result, allowing strings to be used as keys in Dictionary instances and for other purposes. A string is a collection of , which approximate human-readable characters. Many individual characters, such as “é”, “김”, and “🇮🇳”, can be made up of multiple Unicode scalar values. These scalar values are combined by Unicode’s boundary algorithms into extended grapheme clusters, represented by the Swift Character type. Each element of a string is represented by a Character instance. For example, to retrieve the first word of a longer string, you can search for a space and then create a substring from a prefix of the string up to that point:

The firstName constant is an instance of the Substring type—a type that represents substrings of a string while sharing the original string’s storage. Substrings present the same interface as strings.

If you need to access the contents of a string as encoded in different Unicode encodings, use one of the string’s unicodeScalars, utf16, or utf8 properties. Each property provides access to a view of the string as a series of code units, each encoded in a different Unicode encoding. To demonstrate the different views available for every string, the following examples use this String instance:

The cafe string is a collection of the nine characters that are visible when the string is displayed.

A string’s unicodeScalars property is a collection of Unicode scalar values, the 21-bit codes that are the basic unit of Unicode. Each scalar value is represented by a Unicode.Scalar instance and is equivalent to a UTF-32 code unit.

The unicodeScalars view’s elements comprise each Unicode scalar value in the cafe string. In particular, because cafe was declared using the decomposed form of the "é" character, unicodeScalars contains the scalar values for both the letter "e" (101) and the accent character "´" (769). A string’s utf16 property is a collection of UTF-16 code units, the 16-bit encoding form of the string’s Unicode scalar values. Each code unit is stored as a UInt16 instance.

The elements of the utf16 view are the code units for the string when encoded in UTF-16. These elements match those accessed through indexed NSString APIs.

A string’s utf8 property is a collection of UTF-8 code units, the 8-bit encoding form of the string’s Unicode scalar values. Each code unit is stored as a UInt8 instance.

The elements of the utf8 view are the code units for the string when encoded in UTF-8. This representation matches the one used when String instances are passed to C APIs.

When you need to know the length of a string, you must first consider what you’ll use the length for. Are you measuring the number of characters that will be displayed on the screen, or are you measuring the amount of storage needed for the string in a particular encoding? A single string can have greatly differing lengths when measured by its different views. For example, an ASCII character like the capital letter is represented by a single element in each of its four views. The Unicode scalar value of is 65, which is small enough to fit in a single code unit in both UTF-16 and UTF-8.

On the other hand, an emoji flag character is constructed from a pair of Unicode scalar values, like "\u\{1F1F5\}" and "\u\{1F1F7\}". Each of these scalar values, in turn, is too large to fit into a single UTF-16 or UTF-8 code unit. As a result, each view of the string "🇵🇷" reports a different length.

To check whether a string is empty, use its isEmpty property instead of comparing the length of one of the views to 0. Unlike with isEmpty, calculating a view’s count property requires iterating through the elements of the string. To find individual elements of a string, use the appropriate view for your task. For example, to retrieve the first word of a longer string, you can search the string for a space and then create a new string from a prefix of the string up to that point.

Strings and their views share indices, so you can access the UTF-8 view of the name string using the same firstSpace index.

Note that an index into one view may not have an exact corresponding position in another view. For example, the flag string declared above comprises a single character, but is composed of eight code units when encoded as UTF-8. The following code creates constants for the first and second positions in the flag.utf8 view. Accessing the utf8 view with these indices yields the first and second code UTF-8 units.

When used to access the elements of the flag string itself, however, the secondCodeUnit index does not correspond to the position of a specific character. Instead of only accessing the specific UTF-8 code unit, that index is treated as the position of the character at the index’s encoded offset. In the case of secondCodeUnit, that character is still the flag itself.

If you need to validate that an index from one string’s view corresponds with an exact position in another view, use the index’s samePosition(in:) method or the init(_:within:) initializer.

Although strings in Swift have value semantics, strings use a copy-on-write strategy to store their data in a buffer. This buffer can then be shared by different copies of a string. A string’s data is only copied lazily, upon mutation, when more than one string instance is using the same buffer. Therefore, the first in any sequence of mutating operations may cost O() time and space. When a string’s contiguous storage fills up, a new buffer must be allocated and data must be moved to the new storage. String buffers use an exponential growth strategy that makes appending to a string a constant time operation when averaged over many append operations. Any String instance can be bridged to NSString using the type-cast operator (as), and any String instance that originates in Objective-C may use an NSString instance as its storage. Because any arbitrary subclass of NSString can become a String instance, there are no guarantees about representation or efficiency when a String instance is backed by NSString storage. Because NSString is immutable, it is just as though the storage was shared by a copy. The first in any sequence of mutating operations causes elements to be copied into unique, contiguous storage which may cost O() time and space, where is the length of the string’s encoded representation (or more, if the underlying NSString has unusual performance characteristics). For more information about the Unicode terms used in this discussion, see the . In particular, this discussion mentions , , and .

Methods

isHex()

Checks if the string is in Hex format.

func isHex(
) throws -> Bool

Returns

Bool · result of the check.

isBase32()

Checks if the string is in Base32 format.

func isBase32(
) throws -> Bool

Returns

Bool · result of the check.

isBase64()

Checks if the string is in Base64 format.

func isBase64(
) throws -> Bool

Returns

Bool · result of the check.

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

PrivMX Endpoint Swift v2.3

This package is not up to date with the core documentation. Some of the features you've seen described in other parts of the documentation might not be mentioned here. Those changes do not influence compatibility, however