gpt4 book ai didi

ios - 如何在 alamofire 多部分数据中传递字典? iOS swift3

转载 作者:行者123 更新时间:2023-12-04 14:25:14 26 4
gpt4 key购买 nike

我想制作一本字典并将这些数据与使用 alamofire 上传的视频文件一起传递。下面是我的 json。

{
"taskid": "2",
"task_type": "N\/A",
"fileName": null,
“videofile”:
"task": [
{
“Cid": 1,
“Start”: "00:00:00",
“End”: "00:00:00"
},

{
“Cid": 2,
“Start”: "00:00:00",
“End”: "00:00:00"
},
{
“Cid": 3,
“Start”: "00:00:00",
“End”: "00:00:00"
},
]
}

下面字典数组的代码应该是什么?如何通过?

multipartFormData.append((self.singleTonFile?.fileUrl)! , withName: "videofile" , fileName: fileNameOnly + ".webm", mimeType: "video/webm")
multipartFormData.append(self.singleTon.userid.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "userid")

multipartFormData.append(self.singleTon.taskid.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "taskid")

multipartFormData.append(self.singleTon. filename.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "filename")

@里迪。 - 检查我的一个样本的解决方案。这是一种不同的格式。但遵循字典数组作为参数..

for i in 0..<self.singleTon.individualTask.count
{
let dictTask = ["id":self.singleTon.individualTask[i],
"start_time":self.singleTon.individualTaskStartTime[i],
"end_time":self.singleTon.individualTaskEndTime[i],
]
parameters.append(dictTask as [String : AnyObject])
}
]
var dataParameters = Data()
do
{
dataParameters = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions(rawValue: 0))
let objectString = String(data: dataParameters, encoding: .utf8)

print("objectString : ", objectString!)
}
catch
{

}

var alamoDict = [String: Any]()
if let scrn: NSScreen = NSScreen.main() {
let rect: NSRect = scrn.frame
let height = rect.size.height
let width = rect.size.width
self.singleTon.display = "\(height)" + "X" + "\(width)"
}
print("self.singleTon.processor ", self.singleTon.processor)
print("resolution ", self.singleTon.display)
alamoDict = ["task":parameters,
"userid":self.singleTon.userId,
"video_duration":self.singleTon.video_duration,
"fileName":self.singleTon.fileNameForUpload,
"User_type":self.singleTon.User_type,
"OS":self.singleTon.OS,
"processor":self.singleTon.processor,
"task_type":self.singleTon.task_type,
"taskid":self.singleTon.taskIdPassed,
"memory":self.singleTon.memory,
"Freememory":self.singleTon.freememory,
"display":self.singleTon.display,
]

var dataAlamoDict = Data()

//这是为了测试json序列化

    *

do
{
dataAlamoDict = try JSONSerialization.data(withJSONObject: alamoDict, options: JSONSerialization.WritingOptions(rawValue: 0))//NSJSONSerialization.dataWithJSONObject(array, options: nil, error: nil)
let objectString = String(data: dataParameters, encoding: .utf8)

print("objectString : ", objectString!)
}

//This is ur alamofire format


Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(dataAlamoDict, withName: "TaskInfo")
// …… etc ec*

*

最佳答案

帮助像 riddhi 这样的人

@里迪。 - 检查我的一个样本的解决方案。这是一种不同的格式。但遵循字典数组作为参数..

for i in 0..<self.singleTon.individualTask.count
{
let dictTask = ["id":self.singleTon.individualTask[i],
"start_time":self.singleTon.individualTaskStartTime[i],
"end_time":self.singleTon.individualTaskEndTime[i],
]
parameters.append(dictTask as [String : AnyObject])
}
]
var dataParameters = Data()
do
{
dataParameters = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions(rawValue: 0))
let objectString = String(data: dataParameters, encoding: .utf8)

print("objectString : ", objectString!)
}
catch
{

}

var alamoDict = [String: Any]()
if let scrn: NSScreen = NSScreen.main() {
let rect: NSRect = scrn.frame
let height = rect.size.height
let width = rect.size.width
self.singleTon.display = "\(height)" + "X" + "\(width)"
}
print("self.singleTon.processor ", self.singleTon.processor)
print("resolution ", self.singleTon.display)
alamoDict = ["task":parameters,
"userid":self.singleTon.userId,
"video_duration":self.singleTon.video_duration,
"fileName":self.singleTon.fileNameForUpload,
"User_type":self.singleTon.User_type,
"OS":self.singleTon.OS,
"processor":self.singleTon.processor,
"task_type":self.singleTon.task_type,
"taskid":self.singleTon.taskIdPassed,
"memory":self.singleTon.memory,
"Freememory":self.singleTon.freememory,
"display":self.singleTon.display,
]

var dataAlamoDict = Data()

//这是为了测试json序列化格式

    *

do
{
dataAlamoDict = try JSONSerialization.data(withJSONObject: alamoDict, options: JSONSerialization.WritingOptions(rawValue: 0))//NSJSONSerialization.dataWithJSONObject(array, options: nil, error: nil)
let objectString = String(data: dataParameters, encoding: .utf8)

print("objectString : ", objectString!)
}

//This is ur alamofire format


Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(dataAlamoDict, withName: "TaskInfo")
// …… etc ec* type ur rest of the code

*

关于ios - 如何在 alamofire 多部分数据中传递字典? iOS swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47081381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com