我在 Swift 应用程序中使用 KeychainWrapper,并且
KeychainWrapper.standard.bool(forKey: "isUserInSub")
导致了错误。
@State var isUserDeactive: Bool = KeychainWrapper.standard.bool(forKey: "isUserInSub") == true ? false : true
行会导致弹出完全不相关的视图(并且无法关闭)。我还尝试将变量初始化为 false 并将其分配给钥匙串值 .onAppear,但它会导致相同的行为。我在我的主要内容视图中,如果这有什么不同的话。
键值已正确检索和分配,没有错误。它也被成功地检索并在其他组件中使用,没有出现错误。
这是视图的代码,我使用 bool 来渲染屏幕,但是
isUserDeactive = KeychainWrapper.standard.bool(forKey: "isUserInSub") == true ? false : true
导致了错误
struct ContentView: View {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let loadingView = LoadingUI.shared
@EnvironmentObject var appSettings: AppSettings
@State var isUserDeactive: Bool = false
var body: some View {
ZStack {
Group {
if showIntroScreen {
IntroScreenAbo()
} else {
CustomTabBarContainerView(content: {
HomeView()
Top100View)
if isUserDeactive {
AboView()
} else {
MakerView()
MoreView()
})
.background(Color("HeaderBarHome_background"))
.....
.onAppear {
isUserDeactive = KeychainWrapper.standard.bool(forKey: "isUserInSub") == true ? false : true
}
我解决了它,问题是 KeychainWrapper.standard.bool(forKey: "isUserInSub") 返回一个条件 Bool (可选类型“Bool?”的值必须解包为“Bool”类型的值)。我仍然不知道它如何设法渲染随机视图,而不是给出警告/错误,但添加
?? true
解决了问题。