简体   繁体   中英

Create complex [String:AnyObject] for Alamofire parameters

it's my first project with Swift, so be patient. I'm trying to integrate my REST services using Alamofire . I have some troubles with building of the parameters field in case of complex JSON structure.

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.

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]

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.

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:

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 :

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:

{"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

You can easily achieve this easily by using SwiftyJSON

Install it via Cocoapods or manually. For manually you can simply download and add the SwiftyJSON.xcodeproj file in your workspace.

Add SwiftyJSON framework for iOS in Build Phases

在此处输入图片说明

Now simply import it in your ViewController

import SwiftyJSON

Even after importing if Xcode doesn't recognize it.Clean and build it.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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