Swiftui - 显示解码数据以查看时出错
Posted
技术标签:
【中文标题】Swiftui - 显示解码数据以查看时出错【英文标题】:Swiftui - Error displaying decoded data to view 【发布时间】:2021-08-05 07:20:41 【问题描述】:我无法通过传递给我的视图的环境对象将我解码为结构的数据显示到我的视图中。显示的错误消息是“调用初始化程序时没有完全匹配”。我一直想弄明白,但没有运气。
这是我的网络文件的一些代码,用作我解码结构的真实来源
class AuthViewModel: ObservableObject
@Published var user: LoginResponseData.Root? = nil
@Published var alert: CustomAlert? = nil
@Published var claimHistroy: ClaimStruct? = nil
var authentication: AuthenticationCheck? = nil
var showLoader = false
let networkService: NetworkService = NetworkService.shared
func signIn(username: String, password: String)
networkService.signIn(username: username, password: password) (result) in
switch result
case .success(let user):
print("This user last name is: \(user.result.login.userName.name.fullName)")
self.user = user
self.authentication?.updateValidation(success: true)
// Calls method
self.profileSessionMember()
case .failure(let error):
print("The error is: \(error.localizedDescription)")
//Reset the variable
self.user = nil
self.authentication?.updateValidation(success: false)
// Pass a message to the user
self.alert = CustomAlert(title: "Invalid Credentials", message: "Either username or password is incorrect. Please try again")
/// Gets the Search Savings Amouny from decoded struct
func profileSessionMember()
networkService.profileSessionMember result in
switch result
case .success(let userHistory):
// Sets the claimHistory
self.claimHistroy = userHistory
case .failure(let error):
print(error)
class AuthenticationCheck: ObservableObject
@Published var isValidated = false
func updateValidation(success: Bool)
withAnimation
isValidated = success
这是我的身份验证,将 environmentObject 传递给我的视图
@main
struct SomeApp: App
@StateObject var authentication = AuthenticationCheck()
@StateObject var vm: AuthViewModel = AuthViewModel()
var body: some Scene
WindowGroup
if authentication.isValidated
ContentView()
.environmentObject(vm)
else
Signin()
.environmentObject(vm)
.environmentObject(authentication)
//TestView()
这是我试图显示信息的地方
struct SearchHistory: View
@EnvironmentObject var vm: AuthViewModel
var body: some View
VStack
Text("BASED ON YOUR SEARCH HISTORY")
.font(.title3)
.fontWeight(.heavy)
.foregroundColor(Color(red: 0.584, green: 0.655, blue: 0.992, opacity: 100.0))
.multilineTextAlignment(.center)
.padding()
HStack
Image(systemName: "dollarsign.circle")
.resizable()
.frame(width: 43.0, height: 44.0)
.aspectRatio(contentMode: .fill)
.padding()
VStack
Text("Total")
.multilineTextAlignment(.center)
Text("Amount")
.multilineTextAlignment(.center)
// This does not work
Text(vm.claimHistroy?.result.member.yearToDateSearchSavingsAmount)
【问题讨论】:
错误信息出现在哪一行? @Larme 我发布的代码的最后一行。我对哪些代码不起作用有意见 【参考方案1】:Text() 需要在其中包含一些字符串才能工作。由于“vm.claimHistroy”可能为 nil,如果它变为 nil,请尝试在其中包含一些文本。像这样的:
Text(vm.claimHistroy?.result.member.yearToDateSearchSavingsAmount ?? "")
你甚至可以试试这个:
if let history = vm.claimHistroy
Text(history.result.member.yearToDateSearchSavingsAmount)
编辑:
如果“yearToDateSearchSavingsAmount”不是字符串,试试这个:
if let amount = vm.claimHistroy?.result.member.yearToDateSearchSavingsAmount
Text("\(amount)")
【讨论】:
第一个建议显示“无法为表达式生成诊断”的错误,请提交错误报告”并显示相同的错误“调用初始化程序时没有完全匹配” 第二个建议怎么样?这会消除错误吗? “yearToDateSearchSavingsAmount”是一个字符串吗? 我已经为“yearToDateSearchSavingsAmount”不是字符串的情况编辑了答案。以上是关于Swiftui - 显示解码数据以查看时出错的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI 结合 Publisher targetstruct 来解码数据
SwiftUI 简明教程之 Swift Package Manager 的使用
在 SWI Prolog 中使用 process_create/3 使用命令提示符或 shell 时出错