SystemFields
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.
| Protocol | Builds on | Adopted by |
|---|---|---|
| SystemFields | None | ContentTypeModel, ContentTypeDecodable, and the three protocols below |
| EntryFields | SystemFields | EntryDecodable, so EntryModel, TaxonomyModel and your entry models |
| AssetFields | SystemFields | AssetDecodable, so AssetModel |
| GlobalFields | SystemFields | GlobalFieldDecodable, so GlobalFieldModel |
| CachePolicyAccessible | None | Stack, Entry, Asset, ContentType, GlobalField, Taxonomy and every query |
| EndpointAccessible | None | Every model type that fetch(_:) or find(_:) accepts |
Class-Level Properties
SystemFields requires the four properties below.
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| uid | String | Read-only | Not applicable | Unique ID of the item. |
| title | String | Read-only | Not applicable | Title of the item. |
| createdAt | Date? | Read-only | Not applicable | Creation date of the item. |
| updatedAt | Date? | Read-only | Not applicable | Date 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.
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| locale | String | Read-only | Not applicable | Locale code of the returned entry. |
| createdBy | String? | Read-only | Not applicable | UID of the user who created the entry. |
| updatedBy | String? | Read-only | Not applicable | UID 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.
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| createdBy | String? | Read-only | Not applicable | UID of the user who created the asset. |
| updatedBy | String? | Read-only | Not applicable | UID of the user who last updated the asset. |
| fileName | String | Read-only | Not applicable | File name of the asset. |
| fileSize | Double | Read-only | Not applicable | Size of the file. |
| fileType | String | Read-only | Not applicable | Media type of the file. |
| url | String | Read-only | Not applicable | URL 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.
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| maintainRevisions | Bool? | Read-only | Not applicable | maintain_revisions value of the Global field. |
| inbuiltClass | Bool? | Read-only | Not applicable | inbuilt_class value of the Global field. |
| version | Int? | Read-only | Not applicable | _version of the Global field. |
| branch | String? | Read-only | Not 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)
}