'How to creat an Object in a View in swiftUI
iam building a small social media app. i am trying to simulate other user, so i made an class called User with the needed attributes. I created an Main user called appUser, who gets the informations that tipped in and . The problem is how and where do i creat the other user and give them there attributes so i can put them into the friend array of appUser.
class User: ObservableObject{
@Published var username: String = ""
@Published var name: String = ""
var password: String = ""
@Published var email: String = ""
@Published var beschreibung: String = ""
@Published var profilBild: UIImage?
@Published var friends = [User]()
@Published var friendAnfrage = [User]()
@Published var friendAnfragebekommen = [User]()
@Published var feed = [SinglePostView]()
func addFriend(friend: User,appUser: User) {
friend.friendAnfrage.append(appUser)
friendAnfrage(friend: appUser)
}
func newFriend(newFriend: User) {
friends.append(newFriend)
}
func friendAnfrage(friend: User) {
friendAnfragebekommen.append(friend)
}
func makePost(image: UIImage,appUser: User) {
feed.append(SinglePostView(bild: image, ersteller: appUser))
for i in 0..<friends.count {
friends[i].feed.append(SinglePostView(bild: image, ersteller: appUser))
}
}
}
struct Home: View {
@EnvironmentObject var appInfo: AppInformation
@StateObject var appUser = User()
let tim: User = User()
tim.username = "Timm"
tim.addFriend(friend: tim, appUser: appUser)
var body: some View {
ZStack {
if appInfo.finished {
if appInfo.home {
HomeView().environmentObject(appUser)
}
else if appInfo.camera {
MakePostView()
}
else if appInfo.friends {
FriendsView().environmentObject(appUser)
}
else if appInfo.profil {
ProfileView().environmentObject(appUser)
.environmentObject(appInfo)
}
if appInfo.showBar {
NavigationBar()
}
}
else {
ErstellenView().environmentObject(appInfo)
.environmentObject(appUser)
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
