简体   繁体   English

为Alamofire参数创建复杂的[String:AnyObject]

[英]Create complex [String:AnyObject] for Alamofire parameters

it's my first project with Swift, so be patient. 这是我与Swift合作的第一个项目,请耐心等待。 I'm trying to integrate my REST services using Alamofire . 我正在尝试使用Alamofire集成我的REST服务。 I have some troubles with building of the parameters field in case of complex JSON structure. 在复杂的JSON结构的情况下,构建参数字段会遇到一些麻烦。

The server needs a structure like this: 服务器需要这样的结构:

{
  "event": {
    "name": "string",
    "duration": 1,
    "lat": 45.0,
    "lon": 45.0,
    "deadline": "2015-12-01T10:07:14.017Z"
  },
  "proposals": [
    "2015-11-01T10:07:14.017Z"
  ],
  "invitations": [
    {
      "user": 0,
      "phone": "string",
      "email": "string"
    }
  ]
}

where proposals is an array of NSDate and invitations is an array of Dictionary, both with variable length. 建议是NSDate数组,邀请是Dictionary数组,长度都是可变的。

At the moment, I'm trying to create a NSMutableDictionary , populate it with the necessary fields and the end convert it to a [String: AnyObject] 目前,我正在尝试创建一个NSMutableDictionary ,用必要的字段填充它,最后将其转换为[String: AnyObject]

It is an example for the event and proposal fields: 这是事件和投标字段的示例:

var parameters: [String:AnyObject] = [
"event" : "",
"proposals": "",
"invitations": "",
]

if let event = mutableParameters.objectForKey("event") as? [String:AnyObject] {
parameters.updateValue(event, forKey: "event")
}

if let prop = mutableParameters.objectForKey("proposals") as? [String:AnyObject] {
parameters.updateValue(prop, forKey: "proposals")
}

But the second if doesn't entry in the true branch. 但是第二个if不会进入true分支。

Any suggestions? 有什么建议么? If someone have a better solution, this will be well-accepted! 如果有人有更好的解决方案,这将是公认的!

The error is that you're casting mutableParameters.objectForKey("proposals") as a [String:AnyObject] dictionary, but this should be [String] , an array of Strings: 错误是您将mutableParameters.objectForKey("proposals")[String:AnyObject]字典,但这应该是[String] ,它是一个字符串数组:

if let prop = mutableParameters.objectForKey("proposals") as? [String] {
    parameters.updateValue(prop, forKey: "proposals")
}

UPDATE 更新

It's better to just make a Swift dictionary and use NSJSONSerialization : 最好只制作一个Swift字典并使用NSJSONSerialization

let source: [String:AnyObject] = ["event" : ["name": "welcome", "duration": "1", "lat": 45.0,"lon": 45.0,"deadline": "2015-12-01T10:07:14.017Z"], "proposals": ["2015-12-01T10:07:14.017Z"], "invitations": [["user": "eric", "phone": "555", "email": "none"]]]

do {
    let dictionaryAsJSONData = try NSJSONSerialization.dataWithJSONObject(source, options: [])
    // send it to your server, or...
    if let jsonDataAsString = NSString(data: dictionaryAsJSONData, encoding: NSUTF8StringEncoding) {
        // send this
        print(jsonDataAsString)
    }
} catch {
    print(error)
}

Here jsonDataAsString is: 这里jsonDataAsString是:

{"proposals":["2015-12-01T10:07:14.017Z"],"invitations":[{"email":"none","user":"eric","phone":"555"}],"event":{"lon":45,"deadline":"2015-12-01T10:07:14.017Z","duration":"1","lat":45,"name":"welcome"}} {“ proposals”:[“ 2015-12-01T10:07:14.017Z”],“ invitations”:[{“ email”:“ none”,“ user”:“ eric”,“ phone”:“ 555”} ],“ event”:{“ lon”:45,“ deadline”:“ 2015-12-01T10:07:14.017Z”,“ duration”:“ 1”,“ lat”:45,“ name”:“ welcome “}}

Typical JSON handling is messy 典型的JSON处理比较混乱

You can easily achieve this easily by using SwiftyJSON 您可以使用SwiftyJSON轻松实现此目标

Install it via Cocoapods or manually. 通过Cocoapods或手动安装。 For manually you can simply download and add the SwiftyJSON.xcodeproj file in your workspace. 对于手动,您只需在工作区中下载SwiftyJSON.xcodeproj文件并将其添加。

Add SwiftyJSON framework for iOS in Build Phases 在构建阶段为iOS添加SwiftyJSON框架

在此处输入图片说明

Now simply import it in your ViewController 现在只需将其导入您的ViewController

import SwiftyJSON

Even after importing if Xcode doesn't recognize it.Clean and build it. 即使在导入后,如果Xcode无法识别它,请清理并构建它。

Code

 let dic1 = ["name": "string", "duration": "1", "lat": 45.0,"lon": 45.0,"deadline": "2015-12-01T10:07:14.017Z"]
 let dic2 = ["2015-12-01T10:07:14.017Z"]
 let dic3 = [["user": "string", "phone": "1", "email": "asd"]]

 let response = [ "event" : dic1,"proposals": dic2,"invitations": dic3]
 print(JSON(response))

Output 输出量

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM