我写的代码登录用户使用表单数据POST方法,我们登录使用电子邮件和密码,但它显示我的错误:对示例方法“append”的调用中没有精确匹配项
我的代码看起来像这样:
struct FirstSignIn: View {
@Environment(\.dismiss) var dismiss
@State private var driverID = ""
@State private var pinTxt = ""
@State private var edit1 = false
@State private var edit2 = false
var body: some View {
ZStack {
shortBgView()
VStack {
VStack {
Text("Welcome To CabsPoint")
.font(.custom(regFont, size: 28))
.foregroundColor(b80)
.padding(.bottom, 26)
VStack {
ZStack {
TextField("Driver ID", text: $driverID) { edit in
if edit {
edit1 = true
} else {
edit1 = false
}
}
.frame(height: 16)
.padding([.leading, .vertical], 16)
.font(.custom(regFont, size: 12))
.tint(edit1 ? primaryColor : b80)
.foregroundColor(edit1 ? primaryColor : b80)
.border(edit1 ? primaryColor : b80, width: 1, cornerRadius: 6)
ZStack {
Text("Driver ID")
.padding(5)
.font(.custom(regFont, size: 12))
.tint(edit1 ? primaryColor : b80)
.foregroundColor(edit1 ? primaryColor : b80)
.background(.white)
.offset(x: 15, y:-24)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
.padding(.bottom, 30)
ZStack {
TextField("Pin", text: $pinTxt) { edit in
if edit {
edit2 = true
} else {
edit2 = false
}
}
.frame(height: 16)
.padding([.leading, .vertical], 16)
.font(.custom(regFont, size: 12))
.tint(edit2 ? primaryColor : b80)
.foregroundColor(edit2 ? primaryColor : b80)
.border(edit2 ? primaryColor : b80, width: 1, cornerRadius: 6)
ZStack {
Text("Pin")
.padding(5)
.font(.custom(regFont, size: 12))
.tint(edit2 ? primaryColor : b80)
.foregroundColor(edit2 ? primaryColor : b80)
.background(.white)
.offset(x: 15, y:-24)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
NavigationLink(destination: ForgotPasswordViews()) {
Text("Forgot Password?")
.font(.custom(medFont, size: 12))
.foregroundColor(b80)
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
.padding([.leading, .trailing], 32)
.padding(.bottom, 16)
NavigationLink(destination: SecondSignIn()) {
Text("Log In")
.font(.custom(medFont, size: 14))
.foregroundColor(b80)
.padding([.leading, .trailing], 75)
.frame(height: 40)
}
.border(primaryColor, width: 1, cornerRadius: 100)
}
}
.padding([.leading, .trailing], 20)
.padding(.top, 25)
}
.adaptsToKeyboard()
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
NavBackButton(dismiss: self.dismiss)
}
}
}
func logInUser() {
guard let url = URL(string: "muUrl.com") else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
// set headers
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
// create body
let parameters = ["action": "driver_login", "email": driverID, "password": pinTxt]
var body = Data()
for (key, value) in parameters {
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append(Data(value.utf8))
body.append("\r\n")
}
body.append("--\(boundary)--\r\n")
request.httpBody = body
// send request
URLSession.shared.dataTask(with: request) { data, response, error in
// handle response
if let data = data {
if let responseString = String(data: data, encoding: .utf8) {
print("Response: \(responseString)")
}
} else if let error = error {
print("Error: \(error.localizedDescription)")
}
}.resume()
}
}
正如我所描述的,我写了登录的函数:
func logInUser() {
guard let url = URL(string: "http://d.url.com") else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
// set headers
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
// create body
let parameters = ["action": "driver_login", "email": driverID, "password": pinTxt]
var body = Data()
for (key, value) in parameters {
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append(Data(value.utf8))
body.append("\r\n")
}
body.append("--\(boundary)--\r\n")
request.httpBody = body
// send request
URLSession.shared.dataTask(with: request) { data, response, error in
// handle response
if let data = data {
if let responseString = String(data: data, encoding: .utf8) {
print("Response: \(responseString)")
}
} else if let error = error {
print("Error: \(error.localizedDescription)")
}
}.resume()
}
但仍然显示错误:在第17、18、20和22行调用示例方法“append”时没有精确匹配
1条答案
按热度按时间xzlaal3s1#
Data
的append
函数需要Data
的另一个示例,但您正在传递String
示例。在您的例子中,您可以使用
utf8
字符串视图来初始化Data
示例。如果您的字符串包含无法用UTF-8表示的序列,那么即使正确,这也不会起作用。在将
value
添加到body
时已经这样做了。您只需要对其他字符串执行此操作。例如:
您也可以在Data上创建扩展:
然后你可以说
body.append(utf8String:"--\(boundary)\r\n")