Skip to main content

Overview

The Post struct is the core data model for video content in the app. It conforms to Codable for easy serialization and includes video properties, author details, and social engagement metrics.

Properties

id
String
required
Unique identifier for the post
video
String
required
Video file name or reference string
videoURL
URL
Full URL to the video file. Defaults to empty file URL if not provided.
videoFileExtension
String
File extension of the video. Defaults to “mp4” if not provided.
videoHeight
Int
required
Height of the video in pixels
videoWidth
Int
required
Width of the video in pixels
autherID
String
required
Unique identifier of the post author. Mapped from “author” in JSON.
autherName
String
required
Display name of the post author
caption
String
required
Text caption or description for the post
music
String
required
Music track or audio identifier associated with the video
likeCount
Int
required
Number of likes the post has received
shareCount
Int
required
Number of times the post has been shared
commentID
String
required
Identifier linking to the post’s comments collection

Initializers

Standard Initialization

let post = Post(
    id: "post123",
    video: "video_file.mp4",
    videoURL: URL(string: "https://example.com/video.mp4"),
    videoFileExtension: "mp4",
    videoHeight: 1920,
    videoWidth: 1080,
    autherID: "user456",
    autherName: "John Doe",
    caption: "Check out this amazing video!",
    music: "song_xyz",
    likeCount: 1250,
    shareCount: 45,
    commentID: "comments789"
)

Dictionary Initialization

Create a Post from a dictionary (useful for Firebase or API responses):
let dict: [String: Any] = [
    "id": "post123",
    "video": "video_file.mp4",
    "videoURL": "https://example.com/video.mp4",
    "videoHeight": 1920,
    "videoWidth": 1080,
    "author": "user456",
    "autherName": "John Doe",
    "caption": "Amazing video!",
    "music": "song_xyz",
    "likeCount": 1250,
    "shareCount": 45,
    "commentID": "comments789"
]

let post = Post(dictionary: dict)

Methods

dictionary
[String: Any]
Computed property that converts the Post instance to a dictionary representation. Returns an empty dictionary if encoding fails.
let post = Post(...)
let postDict = post.dictionary
// Use for Firebase or API uploads

Codable Conformance

The Post struct conforms to Codable, allowing easy encoding and decoding:
// Encoding
let encoder = JSONEncoder()
if let jsonData = try? encoder.encode(post) {
    // Send jsonData to server
}

// Decoding
let decoder = JSONDecoder()
if let post = try? decoder.decode(Post.self, from: jsonData) {
    // Use the decoded post
}

Usage Example

import Foundation
import AVFoundation

// Create a new post
let newPost = Post(
    id: UUID().uuidString,
    video: "my_video.mp4",
    videoURL: URL(string: "https://storage.example.com/videos/my_video.mp4"),
    videoFileExtension: "mp4",
    videoHeight: 1920,
    videoWidth: 1080,
    autherID: currentUser.uid,
    autherName: currentUser.name,
    caption: "Dancing in the rain! #fun #dance",
    music: "rain_song_123",
    likeCount: 0,
    shareCount: 0,
    commentID: "comment_collection_xyz"
)

// Convert to dictionary for Firebase
let postData = newPost.dictionary
firebaseRef.child("posts").child(newPost.id).setValue(postData)

Build docs developers (and LLMs) love