Protocols

The following protocols are available globally.

  • The decodable to get schema of ContentType.

    See more

    Declaration

    Swift

    public protocol ContentTypeDecodable : SystemFields, Decodable
  • Implement this protocol in order to provide your own custom logger for the SDK to log messages to. Your CustomLogger instance will only be passed message it should log according the set log level.

    See more

    Declaration

    Swift

    public protocol CustomLogger
  • Undocumented

    See more

    Declaration

    Swift

    public protocol EntryQueryable : QueryProtocol
  • A base Query protocol which holds the essentials shared by all query types in the SDK which enable querying against content types, entries and assets.

    See more

    Declaration

    Swift

    public protocol QueryProtocol : AnyObject, CachePolicyAccessible
  • A concrete implementation of BaseQuery which serves as the base class for Query, ContentTypeQuery and AssetQuery.

    See more

    Declaration

    Swift

    public protocol BaseQuery : QueryProtocol, Queryable
  • The base Queryable protocol to fetch instance for ContentType, Asset, and Entry.

    See more

    Declaration

    Swift

    public protocol ResourceQueryable
  • The base Queryable protocol to find collections for content types, assets, and entries.

    See more

    Declaration

    Swift

    public protocol Queryable
  • Use types that conform to QueryableRange to perform queries with the four Range operators

    See more

    Declaration

    Swift

    public protocol QueryableRange
  • Sysyem Fields are available fields for entities in Contentstack.

    See more

    Declaration

    Swift

    public protocol SystemFields : AnyObject
  • The Protocol for creating Entry model.

    See more

    Declaration

    Swift

    public protocol EntryFields : SystemFields
  • The Protocol for creating Asset model.

    See more

    Declaration

    Swift

    public protocol AssetFields : SystemFields
  • The cache policy for while fetching entity.

    See more

    Declaration

    Swift

    public protocol CachePolicyAccessible
  • A protocol enabling strongly typed queries to the Contentstack Delivery API via the SDK.

    See more

    Declaration

    Swift

    public protocol FieldKeysQueryable
  • Classes conforming to this protocol are accessible via an Endpoint.

    See more

    Declaration

    Swift

    public protocol EndpointAccessible
  • Decodable is a powerful Swift standard library feature that developers can use to decode custom types from external representation, such as JSON.

    EntryDecodable is an extension of the decodable protocol that you can use to decode the response to a specific model. By using this protocol, you can define types that will be mapped from your entries of the content type.

    In this guide, we will discuss how we can use the EntryDecodable Protocol in your Swift SDK.

    EntryDecodable Example Usage

    Let’s understand how to use this protocol with the help of a few examples.

    Standard Usage

    We have a content type named Session and to fetch entries of our Session content type from the Swift SDK, we need to create a class named Session that implements the EntryDecodable protocol as follows:

    Example usage:

     class Session: EntryDecodable {
        public enum FieldKeys: String, CodingKey {
            case title, uid, locale, type, speakers
            case createdAt = "created_at"
            case updatedAt = "updated_at"
            case createdBy = "created_by"
            case updatedBy = "updated_by"
            case sessionId = "session_id"
            case desc = "description"
            case sessionTime = "session_time"
        }
    
        var locale: String
        var title: String
        var uid: String
        var createdAt: Date?
        var updatedAt: Date?
        var createdBy: String?
        var updatedBy: String?
        var sessionId: Int
        var desc: String
        var type: String
        var sessionTime: SessionTime
        var speakers: [Speaker]?
        public required init(from decoder: Decoder) throws {
            let container   = try decoder.container(keyedBy: FieldKeys.self)
            uid = try container.decode(String.self, forKey: .uid)
            title = try container.decode(String.self, forKey: .title)
            createdBy = try? container.decode(String.self, forKey: .createdBy)
            updatedBy = try? container.decode(String.self, forKey: .updatedBy)
            createdAt = try? container.decode(Date.self, forKey: .createdAt)
            updatedAt = try? container.decode(Date.self, forKey: .updatedAt)
            locale = try container.decode(String.self, forKey: .locale)
            sessionId = try container.decode(Int.self, forKey: .sessionId)
            desc = try container.decode(String.self, forKey: .desc)
            type = try container.decode(String.self, forKey: .type)
            sessionTime = try container.decode(DateTime.self, forKey: .sessionTime)
            speakers = try container.decode([Speaker].self, forKey: .speakers)
        }
     }
    

    Usage in Referencing

    Let’s say there is another content type in our Stack named Speaker that is referenced in our Session Content Type.

    For this case, we will create a class named Speaker that implements the EntryDecodable protocol as follows: Example usage:

     class Speaker: EntryDecodable {
        public enum FieldKeys: String, CodingKey {
            case title, uid, name, locale
            case createdAt = "created_at"
            case updatedAt = "updated_at"
            case createdBy = "created_by"
            case updatedBy = "updated_by"
            case desc = "description"
        }
    
        var locale: String
        var title: String
        var uid: String
        var createdAt: Date?
        var updatedAt: Date?
        var createdBy: String?
        var updatedBy: String?
        var sessionId: Int
        var desc: String
        var name: String
        public required init(from decoder: Decoder) throws {
            let container   = try decoder.container(keyedBy: FieldKeys.self)
            uid = try container.decode(String.self, forKey: .uid)
            title = try container.decode(String.self, forKey: .title)
            createdBy = try? container.decode(String.self, forKey: .createdBy)
            updatedBy = try? container.decode(String.self, forKey: .updatedBy)
            createdAt = try? container.decode(Date.self, forKey: .createdAt)
            updatedAt = try? container.decode(Date.self, forKey: .updatedAt)
            locale = try container.decode(String.self, forKey: .locale)
            name = try container.decode(String.self, forKey: .name)
            desc = try container.decode(String.self, forKey: .desc)
        }
     }
    

    In the Session class, we have a ‘session_time’ Global Field. To parse it, we need to create a class named SessionTime that implements the Decodable protocol as follows.

    Example:

     class SessionTime: Decodable {
        var startTime: Date?
        var endTime: Date?
    
        public enum CodingKeys: String, CodingKey {
            case startTime = "start_time"
            case endTime = "end_time"
        }
        public required init(from decoder: Decoder) throws {
            let container   = try decoder.container(keyedBy: CodingKeys.self)
            startTime = try? container.decode(Date.self, forKey: .startTime)
            endTime = try? container.decode(Date.self, forKey: .endTime)
        }
     }
    

    Note: If we have fields with Modular block, JSON, or an array of JSON in our content type, we can create a class that implements Decodable.

    Declaration

    Swift

    public protocol EntryDecodable : EndpointAccessible, EntryFields, FieldKeysQueryable, Decodable