gpt4 book ai didi

swift - 无法将 'String' 类型的值转换为 'Binding'

转载 作者:行者123 更新时间:2023-12-04 11:59:14 26 4
gpt4 key购买 nike

我正在尝试创建 subview 并收到此错误:

SignInButtons.swift:54:34: error: cannot convert value of type 'String' to expected argument type 'Binding<String>'
signInUrl: "http://www.apple.com",
^~~~~~~~~~~~~~~~~~~~~~struct

我也得到了这个:
'ContentView' initializer is inaccessible due to 'private' protection level

====================
ContentView: View {

import SwiftUI

struct ContentView: View {
//our color palette
let colorWheel : [String: Color] = [
"darkOrange" : Color.init(hex: "F1615D"),
"mediumOrange" : Color.init(hex: "FF8761"),
"darkYellow" : Color.init(hex: "FFC575"),
"lightYellow" : Color.init(hex: "F1FAC6"),
"brightAqua" : Color.init(hex: "79E6E3"),
"lightAqua" : Color.init(hex: "a8e6cf"),
"limeGreen" : Color.init(hex: "dcedc1"),
"brightPeach" : Color.init(hex: "ff8b94"),
"mediumPeach" : Color.init(hex: "ffaaa5"),
"lightPeach" : Color.init(hex: "ffd3b6"),
]

@State private var backgroundColor:Color
@State private var signInUrl = "http://www.apple.com"
@State private var showModal = false
private var detailSize = CGSize(width: 0, height: UIScreen.main.nativeBounds.height)

var body: some View {
ZStack{
VStack {
AnimatedContentView()
Spacer()

SignInButtons(backgroundColor: self.backgroundColor,
signInUrl: self.$signInUrl,
showModal: self.$showModal)

Spacer().frame(height: CGFloat(100))
}
.onAppear() {
self.backgroundColor = self.colorWheel["darkOrange"]!
}
.foregroundColor(Color.black.opacity(0.7))
.edgesIgnoringSafeArea(.all)

VStack {
AskForEmailView(showModal: self.$showModal)
.offset( self.showModal ? CGSize.zero : detailSize)

}

} //end zStack
}
}



struct ContentView_Previews: PreviewProvider {
enum MyDeviceNames: String, CaseIterable {
case iPhoneXrMax = "iPhone 11 Pro Max"
// case iphoneX = "iPhone X"
// case iPhoneXs = "iPhone Xs"
// case iPhoneXsMax = "iPhone Xs Max"
// case iPad = "iPad Pro (11-inch)"

static var all: [String] {
return MyDeviceNames.allCases.map { $0.rawValue }
}
}
static var previews: some View {
//ContentView()
Group {
ForEach(MyDeviceNames.all, id: \.self) { deviceName in
ContentView(detailSize: CGSize.zero)
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
// ForEach(MyDeviceNames.all, id: \.self) { deviceName in
// ContentView()
// .colorScheme(.dark)
// .background(Color.black)
// .edgesIgnoringSafeArea(.all)
// .previewDevice(PreviewDevice(rawValue: deviceName))
// .previewDisplayName(deviceName)
// }
}

}
}

import SwiftUI

struct SignInButtons: View {
var backgroundColor:Color
@Binding var signInUrl : String
@Binding var showModal : Bool

var body: some View {
VStack(spacing: 0) {
//using text w tap gesture - to do: make into its own component
Text("Sign in with Facebook")
.fontWeight(.light)
.font(.title)
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 70, alignment: .top)
.background(Color.blue.opacity(0.8))
.foregroundColor(Color.white)
.onTapGesture {
self.signInUrl = "http://www.facebook.com"
self.showModal.toggle()
}

//using a button
Button(action: {
print("Button Pushed")
self.signInUrl = "http://www.google.com"
self.showModal.toggle()
}) {
Text("Sign in with Google")
.fontWeight(.light)
.font(.title)
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 70, alignment: .top)
.background(backgroundColor)
.foregroundColor(Color.white)
}
}
.navigationBarHidden(true)
.edgesIgnoringSafeArea([.top, .bottom])
}
}

struct SignInButtons_Previews: PreviewProvider {
static var previews: some View {
SignInButtons(backgroundColor: Color.blue,
signInUrl: "http://www.apple.com",
showModal: false)
}
}

最佳答案

这是可能的方法 - 使用常量绑定(bind)进行预览

struct SignInButtons_Previews: PreviewProvider {
static var previews: some View {
SignInButtons(backgroundColor: Color.blue,
signInUrl: .constant("http://www.apple.com"),
showModal: .constant(false))
}
}

关于swift - 无法将 'String' 类型的值转换为 'Binding<String>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59891263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com