SystemFields

View as Markdown

SystemFields

SystemFields is the protocol for the fields that every Contentstack item carries: uid, title, createdAt and updatedAt. Use this page to

  • see which properties a model of your own must provide
  • write code that works on entries, assets, content types and Global fields alike, through the shared protocol

The page also covers EntryFields, AssetFields, GlobalFields, CachePolicyAccessible and EndpointAccessible. These protocols have no methods. They list properties that a type provides.

Class-Level Notes

Class-bound protocols.

  • SystemFields, and every protocol that builds on it, accepts only classes. A struct model does not compile.

Read-only requirements.

  • Every requirement on this page is { get }. A let or a var property satisfies it.

Who adopts which protocol. The table below lists what each protocol builds on and the types that adopt it.

ProtocolBuilds onAdopted by
SystemFieldsNoneContentTypeModel, ContentTypeDecodable, and the three protocols below
EntryFieldsSystemFieldsEntryDecodable, so EntryModel, TaxonomyModel and your entry models
AssetFieldsSystemFieldsAssetDecodable, so AssetModel
GlobalFieldsSystemFieldsGlobalFieldDecodable, so GlobalFieldModel
CachePolicyAccessibleNoneStack, Entry, Asset, ContentType, GlobalField, Taxonomy and every query
EndpointAccessibleNoneEvery model type that fetch(_:) or find(_:) accepts

Class-Level Properties

SystemFields requires the four properties below.

PropertyTypeAccessDefaultDescription
uidStringRead-onlyNot applicableUnique ID of the item.
titleStringRead-onlyNot applicableTitle of the item.
createdAtDate?Read-onlyNot applicableCreation date of the item.
updatedAtDate?Read-onlyNot applicableDate of the last update to the item.

EntryFields

EntryFields adds the properties of an entry. A model of your own gets these requirements through EntryDecodable, which the Entry class page describes. EntryFields requires the three properties below.

PropertyTypeAccessDefaultDescription
localeStringRead-onlyNot applicableLocale code of the returned entry.
createdByString?Read-onlyNot applicableUID of the user who created the entry.
updatedByString?Read-onlyNot applicableUID of the user who last updated the entry.

AssetFields

AssetFields adds the properties of an asset. AssetModel adopts it through AssetDecodable, which the Asset class page describes. AssetFields requires the six properties below.

PropertyTypeAccessDefaultDescription
createdByString?Read-onlyNot applicableUID of the user who created the asset.
updatedByString?Read-onlyNot applicableUID of the user who last updated the asset.
fileNameStringRead-onlyNot applicableFile name of the asset.
fileSizeDoubleRead-onlyNot applicableSize of the file.
fileTypeStringRead-onlyNot applicableMedia type of the file.
urlStringRead-onlyNot applicableURL of the file.

GlobalFields

GlobalFields adds the properties of a Global field. GlobalFieldModel adopts it through GlobalFieldDecodable, which the Global Fields class page describes. GlobalFields requires the four properties below.

PropertyTypeAccessDefaultDescription
maintainRevisionsBool?Read-onlyNot applicablemaintain_revisions value of the Global field.
inbuiltClassBool?Read-onlyNot applicableinbuilt_class value of the Global field.
versionInt?Read-onlyNot applicable_version of the Global field.
branchString?Read-onlyNot applicable_branch, the branch that holds the Global field.

CachePolicyAccessible

CachePolicyAccessible requires one property, cachePolicy of type CachePolicy. The SDK reads it on each request to decide between the cache and the network.

  • Every type that sends a request adopts it: Stack, Entry, Asset, ContentType, GlobalField, Taxonomy and every query.
  • The protocol requires only read access. Each adopting type declares cachePolicy as a var, so you set it on the object.
  • Each object keeps its own value. The values are .networkOnly, .cacheOnly, .cacheElseNetwork, .networkElseCache and .cacheThenNetwork. The Stack class page describes each one.

EndpointAccessible

EndpointAccessible requires one static property, endpoint of type Endpoint. fetch(_:) and find(_:) read it from the model type you name, to build the request path.

  • The SDK supplies endpoint for every type that adopts EntryDecodable (entries) or AssetDecodable (assets). A model of your own needs no code for it.
  • ContentTypeModel, GlobalFieldModel, TaxonomyModel and SyncStack declare their own endpoint.
  • Because the path comes from the model type, specify a model that matches the request. An entry fetch(_:) with an asset model sends a request for assets. The Contentstack class page lists the Endpoint values.

Method Index

SystemFields and the protocols on this page declare no methods.

Class-Level Snippet

The snippet below prints the shared system fields of entries and of assets with one function, through SystemFields.

import ContentstackSwift
import Foundation

func describe(_ item: SystemFields) {
    print(item.uid, item.title, item.updatedAt ?? "No update date")
}

let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry().query()
    .find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
        switch result {
        case .success(let entries):
            entries.items.forEach(describe)
        case .failure(let error):
            if let apiError = error as? APIError {
                print("Error \(apiError.errorCode): \(apiError.errorMessage)")
            } else {
                print(error)
            }
        }
    }
stack.asset().query()
    .find { (result: Result<ContentstackResponse<AssetModel>, Error>, response: ResponseType) in
        guard case .success(let assets) = result else { return }
        assets.items.forEach(describe)
    }