我写的代码登录用户使用表单数据的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)rn")
body.append("Content-Disposition: form-data; name="(key)"rnrn")
body.append(Data(value.utf8))
body.append("rn")
}
body.append("--(boundary)--rn")
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)rn")
body.append("Content-Disposition: form-data; name="(key)"rnrn")
body.append(Data(value.utf8))
body.append("rn")
}
body.append("--(boundary)--rn")
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'时没有精确匹配
Data
的append
函数需要Data
的另一个实例,但是您传递的是String
实例。
在您的例子中,您可以使用utf8
字符串视图来初始化Data
实例。如果您的字符串包含无法用UTF-8表示的序列,则此操作将无法正常工作。
当您将value
添加到body
时已经这样做了。您只需要对其他字符串执行此操作。
例如:
body.append(Data("--(boundary)rn".utf8))
你也可以在Data上创建一个扩展:
extension Data {
append(utf8String:String) {
self.append(Data(utf8String.utf8))
}
}
然后输入body.append(utf8String:"--(boundary)rn")