将Array添加为字典值

时间:2022-10-28 16:24:48

I am trying to post some parameters to server like this :

我试图将一些参数发布到服务器,如下所示:

["name":"john" , "age":"25"]

but there is key/value which requires a set of array, called tags, when I add it to dictionary like this

但是当我像这样将它添加到字典时,有一个键/值需要一组数组,称为标签

tagsArray = ["#tag1" , "#tag2" , "#tag3"]

["tags":tagsArray] as [String:Any]

my app crashes due to this error :

我的应用程序由于此错误而崩溃:

-[Swift._SwiftDeferredNSArray dataUsingEncoding:]: unrecognized selector sent to instance 0x600000234080

- [Swift._SwiftDeferredNSArray dataUsingEncoding:]:无法识别的选择器发送到实例0x600000234080

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._SwiftDeferredNSArray dataUsingEncoding:]: unrecognized selector sent to instance 0x600000234080'

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [Swift._SwiftDeferredNSArray dataUsingEncoding:]:无法识别的选择器发送到实例0x600000234080'

How should I add Array in dictionary's value ?

我应该如何在字典值中添加数组?

Edited:

let parameters = ["hash":appDefaults.getUserHash() ,  "string": vHash , "media_title":vTitle , "description":vDescription , "Keywords[]":insertTagArray] as [String : Any]

            Alamofire.upload(multipartFormData: { multipartFormData in

                 multipartFormData.append(imgData, withName: "photofileupload",fileName: "file.jpg", mimeType: "image/jpg")

                for (key, value) in parameters {
                    multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
                }
            },
                             to:URLserver)
/// rest of code 

1 个解决方案

#1


0  

When you loop through the dictionary, it crashes because you're trying to encode the array not the strings inside the array. You should also loop through the array of strings and encode them. You can try doing this:

当你遍历字典时,它会崩溃,因为你试图编码数组而不是数组中的字符串。您还应该循环遍历字符串数组并对其进行编码。你可以尝试这样做:

Alamofire.upload(multipartFormData: { multipartFormData in

        multipartFormData.append(imgData, withName: "photofileupload",fileName: "file.jpg", mimeType: "image/jpg")

        for (key, value) in parameters {

            if let tagsArray = value as? [String]{

                let stringsData = NSMutableData()
                for tag in tagsArray{
                    if let stringData = string.dataUsingEncoding(NSUTF8StringEncoding) {
                        stringsData.appendData(stringData)
                    }
                }

                multipartFormData.append(stringsData), withName: key)
            }
            else if let stringValue = value as? String{

                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)

            }

        },to:URLserver)

    }

#1


0  

When you loop through the dictionary, it crashes because you're trying to encode the array not the strings inside the array. You should also loop through the array of strings and encode them. You can try doing this:

当你遍历字典时,它会崩溃,因为你试图编码数组而不是数组中的字符串。您还应该循环遍历字符串数组并对其进行编码。你可以尝试这样做:

Alamofire.upload(multipartFormData: { multipartFormData in

        multipartFormData.append(imgData, withName: "photofileupload",fileName: "file.jpg", mimeType: "image/jpg")

        for (key, value) in parameters {

            if let tagsArray = value as? [String]{

                let stringsData = NSMutableData()
                for tag in tagsArray{
                    if let stringData = string.dataUsingEncoding(NSUTF8StringEncoding) {
                        stringsData.appendData(stringData)
                    }
                }

                multipartFormData.append(stringsData), withName: key)
            }
            else if let stringValue = value as? String{

                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)

            }

        },to:URLserver)

    }