无法将 'Int' 类型的值转换为 SwiftUI/Playgrounds 中预期的参数类型 'string'
Cannot convert value of type 'Int' to expected argument type 'string' in SwiftUI/Playgrounds
有点像 sequel 到 已经回答了。无论如何,我有一个 URL 会话要发送到 webhook。我已经设置了字符串查询,但我尝试使用 Stepper 控件值进行查询,并将其添加到 URLQueryItem
的列表中,但我收到一条错误消息 Cannot convert value of type 'int' to expected argument type 'string'
。所以事实证明步进器中的值实际上是一个整数(或 Int
类型)而不是字符串。
这是代码
struct ContentView: View {
@State private var TweetStatus = ""
@State private var response = ""
@State var showAlert = false
@State var SendToWebhook = false
@State var StepperValue = 0
var body: some View {
NavigationView {
Form {
Section(footer: Text("Test")) {
TextField("Field to place response data", text: $response)
TextEditor( text: $TweetStatus)
.frame(height: 100)
// Stepper
Stepper(value: $StepperValue, in: 0...10080) {
Text("Duration in Minutes: \(StepperValue)")
}
}
Section {
Button("Get Data") {
requestTest() { results in
response = results
if response == "No Data!" {
showAlert = true
}
}
}
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Tweet Sent"), message: Text("Your Tweet is sent! Your Tweet ID is shown in the field"), dismissButton: .default(Text("OK")))
}
}
}
func requestTest(completion: @escaping(String) -> ()) { // <--- here completion closure
if let url = URL(string: "http://requestbin.net/r/dooek323") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.queryItems = [ URLQueryItem(name: "TweetID", value: response),
URLQueryItem(name: "Status", value: TweetStatus),
URLQueryItem(name: "PollDuration", value: StepperValue)]
if let query = components.url!.query {
request.httpBody = Data(query.utf8)
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data,
let apiResponse = String(data: data, encoding: .utf8) {
// IF Completed, these actions are shown below
completion(apiResponse) // <--- here
self.showAlert = true
TweetStatus = ""
} else {
completion("No Data!") // <--- here
}
}
task.resume()
}
}
}
所以我想做的是将 id StepperValue
作为字符串传递给 URLQueryItem
。我什至不能使用 String(SOMETHING)
,也无济于事。我认为它是 Swift 5.5,因此必须对此进行一些代码更改。
首先请以小写字母开头的变量命名。
一个URL实际上是一个字符串,即使是一个数值也必须是一个字符串。
因此将整数显式转换为字符串。
URLQueryItem(name: "PollDuration", value: String(stepperValue))]
有点像 sequel 到 URLQueryItem
的列表中,但我收到一条错误消息 Cannot convert value of type 'int' to expected argument type 'string'
。所以事实证明步进器中的值实际上是一个整数(或 Int
类型)而不是字符串。
这是代码
struct ContentView: View {
@State private var TweetStatus = ""
@State private var response = ""
@State var showAlert = false
@State var SendToWebhook = false
@State var StepperValue = 0
var body: some View {
NavigationView {
Form {
Section(footer: Text("Test")) {
TextField("Field to place response data", text: $response)
TextEditor( text: $TweetStatus)
.frame(height: 100)
// Stepper
Stepper(value: $StepperValue, in: 0...10080) {
Text("Duration in Minutes: \(StepperValue)")
}
}
Section {
Button("Get Data") {
requestTest() { results in
response = results
if response == "No Data!" {
showAlert = true
}
}
}
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Tweet Sent"), message: Text("Your Tweet is sent! Your Tweet ID is shown in the field"), dismissButton: .default(Text("OK")))
}
}
}
func requestTest(completion: @escaping(String) -> ()) { // <--- here completion closure
if let url = URL(string: "http://requestbin.net/r/dooek323") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.queryItems = [ URLQueryItem(name: "TweetID", value: response),
URLQueryItem(name: "Status", value: TweetStatus),
URLQueryItem(name: "PollDuration", value: StepperValue)]
if let query = components.url!.query {
request.httpBody = Data(query.utf8)
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data,
let apiResponse = String(data: data, encoding: .utf8) {
// IF Completed, these actions are shown below
completion(apiResponse) // <--- here
self.showAlert = true
TweetStatus = ""
} else {
completion("No Data!") // <--- here
}
}
task.resume()
}
}
}
所以我想做的是将 id StepperValue
作为字符串传递给 URLQueryItem
。我什至不能使用 String(SOMETHING)
,也无济于事。我认为它是 Swift 5.5,因此必须对此进行一些代码更改。
首先请以小写字母开头的变量命名。
一个URL实际上是一个字符串,即使是一个数值也必须是一个字符串。
因此将整数显式转换为字符串。
URLQueryItem(name: "PollDuration", value: String(stepperValue))]