WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED Trying to @Published a class in swiftUI

Forums > SwiftUI

Hello I´m kinda new in this swiftUI.

I'm trying to use the @published keyword to expose a class but im getting this error

class UserViewModel: ObservableObject {

@Published var user: User = User()

func getUser(with userId: Int) {

$user=User(ID: userId, firstName: "Guillermo", lastName: "Jimenez") }

}

Cannot assign value of type 'User' to type 'Published<User>.Publisher'

Link to the image

   

hi

you just need to remove the $ from this line:

$user=User(ID: userId, firstName: "Guillermo", lastName: "Jimenez") }

that should hopefully sort it

Richard

   

You are right, after some twiking I was able to make it work.

Thank you very much!

   

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.