这是我的代码摘录:
let body:NSMutableString = NSMutableString()
body.appendFormat("\(MPboundary)\r\n" as NSString)
body.appendFormat("Content-Disposition: form-data; name=\"\notifications\"\r\n\r\n")
body.appendFormat("\(jsonString)\r\n\n" as NSString)
let end:String = "\(endMPboundary)"
let myRequestData:NSMutableData = NSMutableData()
myRequestData.append(body.data(using: String.Encoding.utf8.rawValue)!)
myRequestData.append(end.data(using: String.Encoding.utf8)!)
request.httpBody = myRequestData as Data
该行:
request.httpBody = myRequestData as Data
给出错误“无法将'NSMutableData'类型的值转换为强制类型'数据'”
答案 0 :(得分:0)
受到评论的启发,这是一个原生的 Swift (3)版本:
var body = ""
body += "\(MPboundary)\r\n"
body += "Content-Disposition: form-data; name=\"\notifications\"\r\n\r\n"
body += "\(jsonString)\r\n\n"
let end = "\(endMPboundary)"
var myRequestData = Data()
myRequestData.append(body.data(using: .utf8)!)
myRequestData.append(end.data(using: .utf8)!)
request.httpBody = myRequestData
或更简单:
var body = ""
body += "\(MPboundary)\r\n"
body += "Content-Disposition: form-data; name=\"\notifications\"\r\n\r\n"
body += "\(jsonString)\r\n\n"
body += "\(endMPboundary)"
request.httpBody = body.data(using:.utf8)!