You could try make a protocol
protocol Post {
var id: UUID { get }
var date: Date { get set }
var description: String { get set }
}
Then you structs have this protocol
struct ImagePost: Post {
let id = UUID()
var date: Date
var description: String
}
struct VideoPost: Post {
let id = UUID()
var date: Date
var description: String
}
struct TweetPost: Post {
let id = UUID()
var date: Date
var description: String
}
Then the arrays of each type (note that i used Date.now
for each date just for demo)
let imagePosts = [
ImagePost(date: Date.now, description: "ImagePost1"),
ImagePost(date: Date.now, description: "ImagePost2")
]
let videoPosts = [
VideoPost(date: Date.now, description: "VideoPost1"),
VideoPost(date: Date.now, description: "VideoPost2")
]
let tweetPosts = [
TweetPost(date: Date.now, description: "TweetPost1"),
TweetPost(date: Date.now, description: "TweetPost2")
]
Now you can combine them
var combinePosts: [Post] {
imagePosts + videoPosts + tweetPosts
}
Now you can use in List
or ForEach
var body: some View {
List(combinePosts, id: \.id) { post in
Text(post.description)
}
}
PS once you have the combinePost you can then sort it by date.