大佬教程收集整理的这篇文章主要介绍了调用 SwiftUI,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
请帮忙,这让我发疯。我在 Profileviewmodel 中有一个常量(让用户:usermodel) usermodel 在 usermodel 文件中是可识别的我还启动了用户:Profileviewmodel 中的 usermodel。
在 HomeVIEw 中,我添加了 (let profileVM: Profileviewmodel) 常量这允许我检索 用户个人资料图片通过“profileVm.user.profileImageURl.
在我的 MainVIEw 中,我根据是否登录加载 HomeVIEw() 我被要求传递 profileVM 和用户让 HomeVIEw() 中的常量给我一个运行应用程序的错误。
请看我的代码:
@H_933_2@mainVIEw.Swift(这是我被要求提供参数的地方) import UIKit
import SwiftUI
import Firebase
struct MainVIEw: VIEw {
@EnvironmentObject var authviewmodel: Authviewmodel
@AppStorage("current_status") var profileRegistered = false
var body: some VIEw {
ZStack{
if authviewmodel.session != nil && authviewmodel.profileRegistered == false {
RegisterVIEw()
} else {
HomeVIEw()
}
if authviewmodel.session == nil {
NavigationVIEw{
Login()
.navigationbarHIDden(true)
.navigationbarBACkbuttonHIDden(true)
}
}
}
}
}
HomeVIEw.swift(只是顶部)
import SwiftUI
import Firebase
import Kingfisher
struct HomeVIEw: VIEw {
@State var showProfile: Bool = false
@State var userSearch: Bool = false
@EnvironmentObject var authviewmodel: Authviewmodel
let profileVM: Profileviewmodel
var body: some VIEw {
vstack {
HStack(alignment: .top) {
//button TO SHOW FolLOWER SCREEN AND FIND PEOPLE IN THE COM@R_419_5845@Y
button(action: { self.userSearch.toggle() }) {
Image(systemname: "person.crop.circle.badge.plus")
.Font(.system(size: 30))
.foregroundcolor(.black)
}.fullScreenCover(isPresented: $userSearch) {
UserSearch()}
Spacer()
//button TO OPEN UP PROfile
button(action: { self.showProfile.toggle() }) {
KFImage(URL(String: profileVm.user.profileImageURL))
.renderingMode(.original)
.resizable()
.frame(wIDth: 36,height: 36)
.clipShape(Circle())
}.fullScreenCover(isPresented: $showProfilE) {
MyProfileVIEw()}
}
.padding(.horizontal)
.padding(.top,15)
button(action: {
// logging out...
authviewmodel.signOut()
},label: {
Text("logout")
.FontWeight(.heavy)
})
Spacer()
}
}
}
var body: some VIEw {
Profileviewmodel.swift
import SwiftUI
class Profileviewmodel: ObservabLeobject {
// MARK: - @PROPERTIES
//∆..............................
@Published var isFollowed: Bool = false
let user: usermodel
//∆..............................
// MARK: -∆ Initializer
///∆.................................
init(user: usermodel) {
self.user = user
/// ∆ Will persist if following or not even when you close the app
checkIfUserIsFollowed()
}
///∆.................................
///∆ ........... Class Methods ...........
// MARK: -∆ follow •••••••••
func follow() -> VoID {
//∆..........
/// ∆ The current user logged in ID
guard let currentUID = FIREBASE_AUTH.currentUser?.uID else { return }
let followingRef = ColLECTION_FolLOWING_FIRESTORE.document(currentUID).collection("user-following")
let followersRef = ColLECTION_FolLOWERS_FIRESTORE.document(currentUID).collection("user-followers")
/// ∆ updates the following structure when following someone
followingRef.document(user.ID).setData([ : ]) { _ in
//∆..........
/// ∆ updates the followers structure when following someone with the currentUID
followersRef.document(currentUID).setData([ : ]) { _ in
//∆..........
self.isFollowed = true
print("DEBUG: Followed: @\(self.user.lastName)...")
}
}
}/// ∆ END OF: follow
// MARK: -∆ unFollow •••••••••
func unFollow() -> VoID {
//∆..........
/// ∆ The current user logged in ID
guard let currentUID = FIREBASE_AUTH.currentUser?.uID else { return }
let followingRef = ColLECTION_FolLOWING_FIRESTORE.document(currentUID).collection("user-following")
let followersRef = ColLECTION_FolLOWERS_FIRESTORE.document(currentUID).collection("user-followers")
followingRef.document(user.ID).delete { _ in
//∆..........
followersRef.document(currentUID).delete { _ in
//∆..........
self.isFollowed = false
print("DEBUG: UnFollowed: @\(self.user.lastName)...")
}
}
}/// ∆ END OF: unFollow
}
//∆.....................................................
extension Profileviewmodel {
// MARK: -∆ checkIfUserIsFollowed •••••••••
func checkIfUserIsFollowed() -> VoID {
//∆..........
guard let currentUID = FIREBASE_AUTH.currentUser?.uID else { return }
let followingRef = ColLECTION_FolLOWING_FIRESTORE.document(currentUID).collection("user-following")
followingRef.document(user.ID).getdocument { snapShot,_ in
//∆..........
guard let isFollowed = snapShot?.exists else { return }
self.isFollowed = isFollowed
}
}
}
usermodel.swift
import SwiftUI
import Firebase
struct usermodel: IDentifiable {
// MARK: - ∆@PROPERTIES
//∆..............................
var ID = UUID().uuIDString
let headline: String
let profileImageURL: String
let firstname: String
let lastname: String
let email: String
let bio: String
var isCurrentUser: Bool {
Auth.auth().currentUser?.uID == self.ID
}
//∆..............................
//∆.....................................................
///∆ ........... Initializer ...........
init(Dictionary: [String : Any]) {
typealias k = RegistrationKeys
//∆..........
ID = DictionarY[k.uIDKey] as? String ?? ""
headline = DictionarY[k.headline] as? String ?? ""
profileImageURL = DictionarY[k.profileImageURLKey] as? String ?? ""
firstname = DictionarY[k.firstname] as? String ?? ""
lastname = DictionarY[k.lastname] as? String ?? ""
email = DictionarY[k.emailKey] as? String ?? ""
bio = DictionarY[k.bioKey] as? String ?? ""
}
}
RegistrationKeys.swift
import Foundation
struct RegistrationKeys: Hashable {
static let emailKey = "email"
static let headline = "headline"
static let firstname = "firstname"
static let lastname = "lastname"
static let profileImageURLKey = "profileImageURL"
static let bioKey = "bio"
static let uIDKey = "uID"
}
使用 usermodel 数据存储在 Firebase 中的注册函数
// MARK: -∆ registerUser •••••••••
func registerUser(firstname: String,lastname: String,headline: String,bio: String,profileImage: UIImagE) -> VoID {
//∆..........
// MARK: -∆ (1) Upload Image •••••••••
guard let imageData = profileImage.jpegData(compressionQuality: 0.3) else { return }
/// ∆ Unique IDentifIEr for the image being uploaded
let filename = NSUUID().uuIDString
let storageRef = Storage.storage().reference().child(fileName)
storageRef.putData(imageData,Metadata: nil) { _,error in
//∆..........
if let error = error {
print("\nDEBUG: {!!!} [ERROR] Failed to upload image: \(error.localizedDescription) {!!!}")
return
}
/// ∆ Should print out before user is created
print("DEBUG: successfully uploaded user photo...")
/// ∆ Accessing the image `URL` &
storageRef.downloadURL { url,_ in
//∆..........
guard let profileImageURL = url?.absoluteString else { return }
let uID = FIREBASE_AUTH.currentUser!.uID
// MARK: -∆ (4) CreaTing our document Dictionary with keys & values •••••••••
typealias k = RegistrationKeys
let data: [String : String] = [
k.uIDKey: uID,k.headline : headline,k.firstname : firstname,k.lastname : lastname,k.bioKey : bio,k.profileImageURLKey : profileImageURL
]
self.uploadCollectionToFirestore(
data: data,uID: uID,firstname: firstname,lastname: lastname,headline: headline,bio: bio)
withAnimation{self.profileRegistered = truE}
}// ∆ END createuser into firebase
}// ∆ END storageRef.downloadURL
}
}/// ∆ END OF: RegisterVIEw Class
extension Authviewmodel {
// MARK: -∆ ••••••••• registerUser •••••••••
fileprivate func uploadCollectionToFirestore(data: [String : String],uID: String,firstname: String,bio: String) {
isLoading = true
//∆..........
ColLECTION_USERS_FIRESTORE.document(uID).setData(data) { (err) in
if err != nil{
self.isLoading = false
return
}
print("DEBUG: successfully signed up user\n")
}
}
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的调用 SwiftUI全部内容,希望文章能够帮你解决调用 SwiftUI所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。