ContentstackResponse

View as Markdown

ContentstackResponse

ContentstackResponse<ItemType> contains the result of a find(_:) call. Use this class to

  • read the entries, assets, content types, global fields or taxonomy entries that the call returned
  • read the total count of matches, to page through them

You never create a ContentstackResponse. Name it, with your model type, in the result type of find(_:), for example Result<ContentstackResponse<EntryModel>, Error>. ItemType must conform to EndpointAccessible and Decodable, as EntryModel, AssetModel, ContentTypeModel, GlobalFieldModel and TaxonomyModel do.

Class-Level Properties

PropertyTypeAccessDefaultDescription
items[ItemType]Read/write[]The items of this response.
countUInt?Read/writenilTotal number of matches, when you asked for the count.
limitUInt?Read/writenilThe limit value, when the response body carries one.
skipUInt?Read/writenilThe skip value, when the response body carries one.
fields[String: Any]?Read/writenilExtra top-level values, today only content_type.

Class-Level Notes

Where items come from. The endpoint of ItemType selects the key that the SDK reads.

  • Entries: entries, or entry for a single entry.
  • Assets: assets, or asset.
  • Content types: content_types, or content_type.
  • Global fields: global_fields, or global_field.
  • Taxonomy queries: entries.

Count and paging.

  • count contains a value only when the query asks the API for the count, for example with include(params: .count) on an entry query. Otherwise it is nil.
  • The API returns up to 100 items for each request by default. Page through more with the limit(to:) and skip(theFirst:) methods of the query, as in Pagination. The SDK does not request more pages itself.

fields. When an entry query includes the content type, fields["content_type"] contains it as a ContentTypeModel. For a single entry, the SDK sets the contentType property of the entry instead.

Method Index

ContentstackResponse has no public methods. The properties table above lists its values.

Class-Level Snippet

The snippet below finds the entries of a content type with their total count, and prints the number of entries on this page against the total.

Find entries and read the 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)
    .find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
        switch result {
        case .success(let response):
            let total = Int(response.count ?? 0)
            print("\(response.items.count) of \(total) entries")
            response.items.forEach { print($0.title) }
        case .failure(let error):
            if let apiError = error as? APIError {
                print("Error \(apiError.errorCode): \(apiError.errorMessage)")
            } else {
                print(error)
            }
        }
    }