[英]Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols; calling functions with SwiftUI
[英]Swift error: "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols" when calling function to write text
我正在尝试使用完成来编写文本,但是当我尝试运行它时出现错误“类型‘()’不能符合‘视图’;只有结构/枚举/类类型可以符合协议”。 任何帮助都会很棒,谢谢!
VStack {
Image("external_white")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 60, height: 60.0)
Text("External IP")
getExternalAddress("") { (test) -> Any in
Text(test)
}
}
调用函数:
func getExternalAddress(_ key: String, completion: @escaping (String) -> Any) {
var test = key
Ipify.getPublicIPAddress { result in
switch result {
case .success(let ip):
print(ip + " ip") // "210.11.178.112"
test = ip
print(test + " test")
completion(test)
case .failure(let error):
print(error.errorDescription)
}
}
你不应该试图在你的视图中做任何事情,SwiftUI 引入了几种在对象之间发布数据的方法,所以请使用它。
在这个例子中,我将你的函数移到了另一个可以观察的类,它发布了找到的 IP 地址
class IpFinder: ObservableObject {
@Published var ipAddress: String = ""
func getExternalAddress(_ key: String) {
var test = key
Ipify.getPublicIPAddress { result in
switch result {
case .success(let ip):
ipAddress = ip
case .failure(let error):
print(error.errorDescription)
}
}
}
}
然后在你的视图中观察这个类的一个实例
struct ContentView: View {
@ObservedObject var ipFinder: IPFinder = IPFinder()
@State var value = 0
@State private var key = ""
var body: some View {
VStack {
Image("external_white")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 60, height: 60.0)
Text("External IP")
Text(ipFinder.ipAddress)
TextField("key", text:$key)
Button(action: {
ipFinder.getExternalAddress(key)
}, label: {
Text("Get IP address")
})
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.