UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: PLEASE HELP getting error (Cannot convert value of type '[PostComment]' to expected argument type '[CommentLike]')

Forums > Swift

I am trying to make an app like instagram and I'm following along on this YouTube video but once I type all the same stuff in this error pops up(Cannot convert value of type '[PostComment]' to expected argument type '[CommentLike]') and I need help I don't know what I did wrong(the code at the bottom is the one that is messing up)

 private func configureModels() {
        guard let userPostModel = self.model else {
            return
        }
        // header
        renderModels.append(PostRenderViewModel(renderType: .header(provider: userPostModel.owner)))
        //post
        renderModels.append(PostRenderViewModel(renderType: .primaryContent(provider: userPostModel)))
        //actions
        renderModels.append(PostRenderViewModel(renderType: .actions(provider: "")))
        // 4 comments
        var comments = [PostComment]()
         for x in 0..<4 {
            comments.append(PostComment(identifier: "123_\(x)", username: "@dave", text: "good job", createdDate: Date(), likes: []))
         }

        renderModels.append(PostRenderViewModel(renderType: .comments(comments: comments)))

    }

2      

is it this line which gives the error?

 comments.append(PostComment(identifier: "123_\(x)", username: "@dave", text: "good job", createdDate: Date(), likes: []))

Could we see your PostComment struct / class definition please?

2      

its the line (the first one presented)and here is the struct for both PostComment and Commentlike(They are made on a different file just to clarify)

 renderModels.append(PostRenderViewModel(renderType: .comments(comments: comments)))

    }

    struct CommentLike {
let username: String
let commentIdentifier: String
}
struct PostComment {
let identifier: String
let username: String
let text: String
let createdDate: Date
let likes: [CommentLike]

}

2      

ah ok, sorry in that case, please could you provide your PostRenderViewModel and the enum you are using for renderType (I think it's an enum by the looks of it)

2      

first is the PostRenderViewModel and then after is the enum


struct PostRenderViewModel {
    let renderType: PostRenderType
}

enum PostRenderType {
    case header(provider: User)
    case primaryContent(provider: UserPost)// post
    case actions(provider: String) // like, comment, share or shop
    case comments(comments: [CommentLike])
}

2      

not on my computer at the moment so cant copy code very easily. hopefully I can explain ok without doing so.

The issue is that when you specify render type of .comments then according to your enum, you are expected to pass in an array of commentLike.

however, you are passing in the comments variable which youve declared as an array of PostComment (first screenshot).

difficult to advice whats its supposed to be but an educated guess says that this supposed to be "comments" rather than "likes" so have you maybe mistyped the expected argument on your comments enum case? should it be instead

case comments(comments: [PostComment])

2      

thank you so much! cant belieave it was that minor of a fix

2      

no problem at all.

sometimes it takes longer to find the issue than fix it :)

Ive spent days finding a solution to a problem before and its like 12 words of code

Richard

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.