EntryQueryable
EntryQueryable
EntryQueryable is the protocol that gives Entry and Query their shared request options. Use its methods to
- request a locale
- return only some fields, or leave some fields out
- resolve reference fields
- add include flags such as locale fallback, the content type schema, metadata and embedded items
- request entry variants
Each method records a setting and returns the same object. The setting takes effect on the fetch(_:) or find(_:) call that follows.
Instance State
You never create an EntryQueryable. You call its methods on one of three types:
- An Entry from stack.contentType(uid:).entry(uid:). Call fetch(_:) to send the request for one entry.
- A Query from query() on an entry. Call find(_:) to send the request for a list of entries.
- A QueryOn<EntryType> from query(_:) on an entry. Call find(_:) as on a Query.
Every method returns Self, the type you call it on. An Entry stays an Entry, and a QueryOn<Product> stays a QueryOn<Product>, so the chain keeps the methods of that type.
Class-Level Notes
Settings stay on the object.
- Each method writes to the parameters dictionary of the object, or to its headers dictionary for the variants methods. No method makes a request.
- Every later fetch(_:) or find(_:) on the same object sends the settings again.
- No method removes a setting. Remove the key from the public parameters or headers dictionary, or create a new entry or query.
- Settings on an Entry do not reach the Query that query() returns. Set them on the query.
Field lists add up, single values replace.
- only(fields:), except(fields:), includeReference(with:) and the two includeReferenceField methods add to the list from an earlier call. The SDK skips a field that the list already contains.
- locale(_:) and the variants methods replace the value from an earlier call.
Where errors surface.
- No method on this page checks its arguments. The API reports an unknown locale, field or variant on the fetch(_:) or find(_:) call that follows.
Capability Matrix
| Capability | Supported | Notes / Alternative |
|---|---|---|
| Resolve every reference without naming the fields | No dedicated method | On a query, call addURIParam(with: "include_all", value: "true"). On an entry, set entry.parameters["include_all"] = "true". See Include All References |
| Return nested embedded items (RECURSIVE) | No | include(params: .embeddedItems) always sends BASE |
| Request unpublished entries | No | include(params: .unpublished) sends no parameter |
| Turn off an include flag | No | Remove the key from parameters, or create a new entry or query |
Method Index
Every method returns the object you call it on and takes effect on the next fetch(_:) or find(_:).
Choose the locale and the fields. Available on an Entry, a Query and a QueryOn<EntryType>.
| Method Name | Returns | Description |
|---|---|---|
| locale(_:) | The same entry or query, for chaining. | Requests entries in one locale. |
| only(fields:) | The same entry or query, for chaining. | Returns only the named top-level fields. |
| except(fields:) | The same entry or query, for chaining. | Leaves the named top-level fields out. |
Resolve reference fields. Available on an Entry, a Query and a QueryOn<EntryType>.
| Method Name | Returns | Description |
|---|---|---|
| includeReference(with:) | The same entry or query, for chaining. | Resolves the named reference fields. |
| includeReferenceField(with:only:) | The same entry or query, for chaining. | Resolves a reference with only the named fields. |
| includeReferenceField(with:except:) | The same entry or query, for chaining. | Resolves a reference without the named fields. |
Add include flags. Available on an Entry, a Query and a QueryOn<EntryType>.
| Method Name | Returns | Description |
|---|---|---|
| include(params:) | The same entry or query, for chaining. | Adds count, content type, fallback and other flags. |
Request entry variants. Available on an Entry, a Query and a QueryOn<EntryType>.
| Method Name | Returns | Description |
|---|---|---|
| variants(uid:) | The same entry or query, for chaining. | Requests one entry variant. |
| variants(uids:) | The same entry or query, for chaining. | Requests several entry variants in priority order. |
| variants(uid:branch:) | The same entry or query, for chaining. | Requests one entry variant from a branch. |
| variants(uids:branch:) | The same entry or query, for chaining. | Requests several entry variants from a branch. |
Class-Level Snippet
The snippet below fetches a French entry with an English fallback, returns three fields, and resolves the author reference with only its title.
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.locale("fr-fr")
.include(params: .fallback)
.only(fields: ["url", "summary", "author"])
.includeReferenceField(with: "author", only: ["title"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
print(entry.title, entry.locale)
let authors = (entry.fields?["author"] as? [Any])?.compactMap { $0 as? EntryModel } ?? []
for author in authors {
print("Author: \(author.title)")
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Methods
Each method of the EntryQueryable protocol has its own section below, with its parameters, validation rules, behavior, and examples.
except(fields:)
except(fields:) leaves the top-level fields you name out of the response.
| Name | Type | Description |
|---|---|---|
| fields (required) | [String] | UIDs of the top-level fields to leave out. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. When the list is empty, or holds only title and locale, the SDK sends no except parameter.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from except(fields:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. except(fields:) writes the list to the except key of parameters under BASE and makes no request.
- except(fields:) removes title and locale from your list without a warning, because EntryModel cannot decode an entry without them.
- The next fetch(_:) or find(_:) sends the list as except[BASE][]. The API returns every field of each entry except those. See the Exclude Operator.
- To name a field inside a Group field, join the UIDs with a dot, such as seo.meta_description. See Exclude Operator Within Group.
- A second call adds its fields to the list. The SDK skips a field that the list already contains.
- To leave fields out of a referenced entry, use the includeReferenceField(with:except:) method.
Example
Leave the body out of a list of entries
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.except(fields: ["body", "seo"])
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
switch result {
case .success(let entries):
for entry in entries.items {
print(entry.title, entry.fields?["body"] == nil)
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Leave a field out of one entry
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.except(fields: ["body"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
guard case .success(let entry) = result else { return }
print(entry.title, entry.fields?.keys.sorted() ?? [])
}include(params:)
include(params:) adds include flags, such as the entry count, the content type schema or the fallback locale, to the next request.
| Name | Type | Description |
|---|---|---|
| params (required) | Query.Include | Flags to add, as one value or a set such as [.count, .fallback]. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. An empty set [] adds nothing.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from include(params:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. include(params:) writes up to two keys to parameters for each flag and makes no request. The next fetch(_:) or find(_:) sends them as query parameters.
- The table below lists the parameters that include(params:) writes for each Query.Include value. The Query class page describes the values.
| Value | Parameters the SDK sends |
|---|---|
| .count | include_count=true |
| .unpublished | None |
| .contentType | include_content_type=true and include_global_field_schema=false |
| .globalField | include_content_type=true and include_global_field_schema=true |
| .refContentTypeUID | include_reference_content_type_uid=true |
| .fallback | include_fallback=true |
| .embeddedItems | include_embedded_items[]=BASE |
| .metadata | include_metadata=true |
| .all | Every parameter above |
- .unpublished has no effect. include(params:) writes no parameter for it, so .all sends no unpublished flag either.
- .count adds the total number of matching entries to a find(_:) response. Read it from count on the ContentstackResponse. See Count. On an Entry, fetch(_:) returns only the entry, so the count does not reach your code.
- .fallback returns the published content of the fallback locale when the requested locale has none. See Get a single entry.
- .embeddedItems returns the entries and assets embedded in Rich Text Editor fields, one level deep. See Get information on embedded RTE objects.
- .metadata adds the _metadata key to each entry. Read it from fields. See Get all entries.
- .contentType and .globalField ask for the content type with the response:
- After fetch(_:), EntryModel holds it in its contentType property. A model of your own receives it when it adopts ContentTypeIncludable. See Entry.
- After find(_:), the content type is in fields["content_type"] of the ContentstackResponse, as a ContentTypeModel.
- .globalField also asks for the schema of the Global fields in the content type.
- Calls add up. A later call never removes a flag, with one exception: a call with .contentType and without .globalField sets include_global_field_schema back to false.
Example
Find entries with their total count
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.include(params: [.count, .fallback])
.limit(to: 10)
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
switch result {
case .success(let entries):
print("Showing \(entries.items.count) of \(entries.count ?? 0) entries")
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Fetch an entry with its content type
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.include(params: .contentType)
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
guard case .success(let entry) = result else { return }
print(entry.title, entry.contentType?.title ?? "")
}Fetch an entry with metadata and embedded items
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.include(params: [.metadata, .embeddedItems])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
guard case .success(let entry) = result else { return }
print(entry.fields?["_metadata"] ?? "No metadata")
print(entry.fields?["_embedded_items"] ?? "No embedded items")
}includeReference(with:)
includeReference(with:) resolves the reference fields you name, so the response holds the referenced entries instead of their UIDs.
| Name | Type | Description |
|---|---|---|
| with (required) | [String] | UIDs of the reference fields to resolve. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. An empty array adds no field.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from includeReference(with:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeReference(with:) writes the list to the include key of parameters and makes no request.
- The next fetch(_:) or find(_:) sends one include[] parameter per field. Without it, a reference field holds only the UIDs of the referenced entries. See Include Reference.
- For a reference field inside a Group field, join the UIDs with a dot, such as bank_offers.bank. For one inside Modular Blocks, name the Modular Blocks field, the block and the field, such as additional_info.related_products.products. See Include Reference Within Group.
- A second call adds its fields to the list. The SDK skips a field that the list already contains.
- In EntryModel.fields, the SDK decodes each resolved entry as an EntryModel and each resolved asset as an AssetModel. A reference field that holds several entries becomes an array [Any] of them. Call toJSON() on the EntryModel to get plain dictionaries. See Entry.
- To name the fields that each referenced entry returns, use the includeReferenceField(with:only:) method.
Example
Resolve the author of an entry
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.includeReference(with: ["author"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
let authors = (entry.fields?["author"] as? [Any])?.compactMap { $0 as? EntryModel } ?? []
for author in authors {
print("Author: \(author.title)")
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Resolve two reference fields on a list of entries
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.includeReference(with: ["author", "categories"])
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
guard case .success(let entries) = result else { return }
for entry in entries.items {
let categories = (entry.fields?["categories"] as? [Any])?.compactMap { $0 as? EntryModel } ?? []
print(entry.title, categories.map { $0.title })
}
}includeReferenceField(with:except:)
includeReferenceField(with:except:) resolves one reference field and leaves the fields you name out of each referenced entry.
| Name | Type | Description |
|---|---|---|
| with (required) | String | UID of the reference field to resolve. |
| except (required) | [String] | Fields to leave out of each referenced entry. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. With an empty except list, includeReferenceField(with:except:) resolves the reference with all its fields.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from includeReferenceField(with:except:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeReferenceField(with:except:) makes no request. It records two settings:
- It adds the field to the include list, the same way the includeReference(with:) method does.
- It writes the except list to the except key of parameters, under the reference field UID.
- The next fetch(_:) or find(_:) sends include[] and except[<reference_field_uid>][]. The API returns every field of each referenced entry except those. See the Exclude Operator.
- Unlike except(fields:), includeReferenceField(with:except:) keeps title and locale in the list. Leave both out of the list to read the referenced entries as EntryModel:
- A referenced entry without locale does not decode as an EntryModel. The SDK stores it in fields as a ContentTypeModel when it has a title.
- A referenced entry without title stays a dictionary in fields.
- A second call with the same field adds its fields to that field's list. Calls with other fields add their own lists.
- The except parameter also holds the list from except(fields:), under the BASE key.
Example
Leave the author biography out
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.includeReferenceField(with: "author", except: ["biography"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
let authors = (entry.fields?["author"] as? [Any])?.compactMap { $0 as? EntryModel } ?? []
for author in authors {
print(author.title, author.fields?["biography"] == nil)
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}includeReferenceField(with:only:)
includeReferenceField(with:only:) resolves one reference field and returns only the fields you name from each referenced entry.
| Name | Type | Description |
|---|---|---|
| with (required) | String | UID of the reference field to resolve. |
| only (required) | [String] | Fields to return from each referenced entry. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. With an empty only list, includeReferenceField(with:only:) resolves the reference with all its fields.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from includeReferenceField(with:only:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeReferenceField(with:only:) makes no request. It records two settings:
- It adds the field to the include list, the same way the includeReference(with:) method does.
- It writes the only list to the only key of parameters, under the reference field UID.
- The next fetch(_:) or find(_:) sends include[] and only[<reference_field_uid>][]. The API returns only the named fields of each referenced entry. See the Only Operator.
- Unlike only(fields:), includeReferenceField(with:only:) does not add title and locale to the list. Add both to the list to read the referenced entries as EntryModel:
- A referenced entry without locale does not decode as an EntryModel. The SDK stores it in fields as a ContentTypeModel when it has a title.
- A referenced entry without title stays a dictionary in fields.
- A second call with the same field adds its fields to that field's list. Calls with other fields add their own lists.
- The only parameter also holds the list from only(fields:), under the BASE key.
Example
Return only the author name and email
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.includeReferenceField(with: "author", only: ["title", "locale", "email"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
let authors = (entry.fields?["author"] as? [Any])?.compactMap { $0 as? EntryModel } ?? []
for author in authors {
print(author.title, author.fields?["email"] ?? "")
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Limit the fields of the entry and of its reference
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.only(fields: ["url", "author"])
.includeReferenceField(with: "author", only: ["title", "locale"])
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
guard case .success(let entries) = result else { return }
for entry in entries.items {
let authors = (entry.fields?["author"] as? [Any])?.compactMap { $0 as? EntryModel } ?? []
print(entry.title, authors.map { $0.title })
}
}locale(_:)
locale(_:) sets the locale of the entries that the next request returns.
| Name | Type | Description |
|---|---|---|
| locale (required) | String | Locale code to request, such as fr-fr. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. locale(_:) accepts any string, including an empty one.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from locale(_:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. locale(_:) writes the value to the locale key of parameters and makes no request.
- The next fetch(_:) or find(_:) sends it as the locale query parameter of the Get a single entry or Get all entries request. The API returns only entries published in that locale.
- Without locale(_:), the SDK sends no locale parameter and the API applies its own default locale.
- A second call replaces the value of the first.
- When the entry has no published content in the locale, the API can return the content of the fallback locale instead. Add .fallback with the include(params:) method to ask for it. Read locale on each returned entry to see which locale the API used.
Example
Fetch an entry in French
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.locale("fr-fr")
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
print(entry.title, entry.locale)
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Find French entries with a fallback locale
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.locale("fr-fr")
.include(params: .fallback)
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
guard case .success(let entries) = result else { return }
for entry in entries.items {
// entry.locale holds the locale the API returned, French or the fallback.
print(entry.title, entry.locale)
}
}only(fields:)
only(fields:) limits the response to the top-level fields you name.
| Name | Type | Description |
|---|---|---|
| fields (required) | [String] | UIDs of the top-level fields to return. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. An empty array still sends title and locale, as Behavior below describes.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from only(fields:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. only(fields:) writes the list to the only key of parameters under BASE and makes no request.
- only(fields:) always adds title and locale to your list, because EntryModel cannot decode an entry without them.
- The next fetch(_:) or find(_:) sends the list as only[BASE][]. The API returns only those fields of each entry. See the Only Operator.
- To name a field inside a Group field, join the UIDs with a dot, such as seo.meta_title. See Only Operator Within Group.
- A second call adds its fields to the list. The SDK skips a field that the list already contains.
- To limit the fields of a referenced entry, use the includeReferenceField(with:only:) method. Its list lives in the same only parameter, under the reference field UID.
- A model of your own fails to decode, and the result is .failure with a DecodingError, when a non-optional field it declares is missing from the response. Declare a field that only(fields:) leaves out as optional, or decode it with try?.
Example
Return only the URL and summary
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.only(fields: ["url", "summary"])
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
switch result {
case .success(let entries):
for entry in entries.items {
print(entry.title, entry.fields?["url"] ?? "")
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Return one field of a Group field
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.only(fields: ["seo.meta_title"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
guard case .success(let entry) = result else { return }
let seo = entry.fields?["seo"] as? [String: Any]
print(entry.title, seo?["meta_title"] ?? "")
}variants(uid:)
variants(uid:) requests one entry variant, by its UID or alias, on the next request.
| Name | Type | Description |
|---|---|---|
| uid (required) | String | UID or alias of the entry variant. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. variants(uid:) sends any string, including an empty one.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from variants(uid:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. variants(uid:) writes the value to the x-cs-variant-uid key of headers and makes no request.
- On an Entry, fetch(_:) sends the header with the Get single entry variant request. The API applies the variant's changes to the fields of the base entry.
- On a query, find(_:) sends the header with the Get multiple variants of an entry request. The API applies the variant to each entry that has it.
- A Personalize variant alias works in place of a variant UID.
- All four variants methods write the same header, so the later call replaces the value of the earlier one.
- variants(uid:) leaves the branch header alone. To request the variant from a branch, use the variants(uid:branch:) method.
- To request the base entry again on the same object, remove the header: entry.headers.removeValue(forKey: "x-cs-variant-uid").
Note The Delivery API offers Entry Variants through an Early Access Program, so the feature may not be available on your stack. See Entry Variants.
Example
Fetch one entry variant
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.variants(uid: "VARIANT_UID")
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
print(entry.title)
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Find entries with one variant applied
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.variants(uid: "VARIANT_UID")
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
guard case .success(let entries) = result else { return }
for entry in entries.items {
print(entry.title)
}
}variants(uid:branch:)
variants(uid:branch:) requests one entry variant from a branch on the next request.
| Name | Type | Description |
|---|---|---|
| uid (required) | String | UID or alias of the entry variant. |
| branch (required) | String | UID of the branch to read the variant from. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. variants(uid:branch:) sends any strings, including empty ones.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from variants(uid:branch:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. variants(uid:branch:) writes the variant to the x-cs-variant-uid key and the branch to the branch key of headers, and makes no request.
- On an Entry, fetch(_:) sends both headers with the Get single entry variant request. On a query, find(_:) sends them with the Get multiple variants of an entry request.
- The branch header on this object replaces the branch you passed to Contentstack.stack(...) for requests from this object only. URLSession gives a header set on the request precedence over the same header in the session configuration.
- A later call to variants(uid:) or variants(uids:) replaces the variant and keeps this branch.
- All four variants methods write the same x-cs-variant-uid header, so the later call replaces the variant of the earlier one.
Note The Delivery API offers Entry Variants through an Early Access Program, so the feature may not be available on your stack. See Entry Variants.
Example
Fetch an entry variant from a branch
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.variants(uid: "VARIANT_UID", branch: "BRANCH_UID")
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
print(entry.title)
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}variants(uids:)
variants(uids:) requests several entry variants, by UID or alias, on the next request.
| Name | Type | Description |
|---|---|---|
| uids (required) | [String] | UIDs or aliases of the variants, highest priority first. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. An empty array sends an empty x-cs-variant-uid header.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from variants(uids:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. variants(uids:) joins the values with commas, writes them to the x-cs-variant-uid key of headers and makes no request.
- On an Entry, fetch(_:) sends the header with the Get single entry variant request. On a query, find(_:) sends it with the Get multiple variants of an entry request.
- The order of the array sets the priority. The API applies the first variant in the list to the base entry fields first.
- By default, the API accepts up to five variant UIDs or aliases in one request.
- Variant UIDs and Personalize variant aliases can appear in the same array.
- All four variants methods write the same header, so the later call replaces the value of the earlier one.
- variants(uids:) leaves the branch header alone. To request the variants from a branch, use the variants(uids:branch:) method.
Note The Delivery API offers Entry Variants through an Early Access Program, so the feature may not be available on your stack. See Entry Variants.
Example
Fetch an entry with two variants
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
// The first variant has the highest priority.
.variants(uids: ["VARIANT_UID", "VARIANT_ALIAS"])
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
print(entry.title)
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}variants(uids:branch:)
variants(uids:branch:) requests several entry variants from a branch on the next request.
| Name | Type | Description |
|---|---|---|
| uids (required) | [String] | UIDs or aliases of the variants, highest priority first. |
| branch (required) | String | UID of the branch to read the variants from. |
Returns: Self. The same entry or query, for chaining.
Validation
- There is no client-side validation. An empty uids array sends an empty x-cs-variant-uid header.
- Errors that do occur come from the find(_:) or fetch(_:) call that follows, not from variants(uids:branch:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. variants(uids:branch:) joins the variants with commas, writes them to the x-cs-variant-uid key and the branch to the branch key of headers, and makes no request.
- On an Entry, fetch(_:) sends both headers with the Get single entry variant request. On a query, find(_:) sends them with the Get multiple variants of an entry request.
- The order of the array sets the priority, as on the variants(uids:) method. By default, the API accepts up to five values.
- The branch header on this object replaces the branch you passed to Contentstack.stack(...) for requests from this object only. URLSession gives a header set on the request precedence over the same header in the session configuration.
- All four variants methods write the same x-cs-variant-uid header, so the later call replaces the variants of the earlier one.
Note The Delivery API offers Entry Variants through an Early Access Program, so the feature may not be available on your stack. See Entry Variants.
Example
Find entries with two variants from a branch
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
.variants(uids: ["VARIANT_UID", "VARIANT_ALIAS"], branch: "BRANCH_UID")
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
switch result {
case .success(let entries):
for entry in entries.items {
print(entry.title)
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}