0

I want to hit an api using Alamofire in which I have to pass parameters as :

"reminder":
[
  {
    "isAlarmOn": "true",
    "time": "06:29 PM"
  },
  {
    "isAlarmOn": "true",
    "time": "06:30 AM"
  }
]

For achieving this, I Created an Array as

var reminderArray : [AlarmRequestModel] = [AlarmRequestModel]()

where AlarmRequestModel is Like ,

AlarmRequestModel.swift

import Foundation
class AlarmRequestModel {
    var time : String = "12:00 PM"

    init(time : String){
        if !time.isEmpty{
            self.time = time
        }

    }
}

and filled reminderArray as

func setArray() {
        reminderArray.insert(AlarmRequestModel(time: "5:00 PM"), atIndex: 0)
        reminderArray.insert(AlarmRequestModel(time: "6:00 PM"), atIndex: 1)
        reminderArray.insert(AlarmRequestModel(time: "7:00 PM"), atIndex: 2)
        reminderArray.insert(AlarmRequestModel(time: "8:00 PM"), atIndex: 3)
    } 

And while hitting the Api , I Declared the parameters as

 func hitApi()
    {
        let parameters  : [String:AnyObject] = [
            "reminder" : reminderArray.description,
            ]

        print("parmeters : \(parameters)")
    }

But I didn't get the expected output , the output was like ->

 parmeters :["reminder": [My_Swaasth.AlarmRequestModel,My_Swaasth.AlarmRequestModel,My_Swaasth.AlarmRequestModel,My_Swaasth.AlarmRequestModel]]

Please anyone Suggest , What changes Do I Need to do in order to achieve the desired output.

1

2 Answers 2

1

There is a protocoll called CustomStringConvertible which has the

var description: String property

But that won't result the desired output. So I'd rather implement a parameter value for AlarmRequestModel

extension AlarmRequestModel {
     var parameterValue: Dictionary<String, String> {
         return ["isAlarmOn": "true", "time": self.time]
     }
}

And use this like

reminderArray.map() { reminder in return reminder.parameterValue }

here:

    let parameters  : [String:AnyObject] = [
        "reminder" : reminderArray.map() { reminder in return reminder.parameterValue },
        ]
Sign up to request clarification or add additional context in comments.

2 Comments

now m getting this output .. "reminder": <_TtCs21_SwiftDeferredNSArray 0x16c6b2f0>( { time = "9:00 AM"; }, { time = "2:31 PM"; }, { time = "9:00 PM"; } )
It is because you use [String:AnyObject], you can change it [String:Any] and the output should be good. Otherwise if you want to parse it with NSJSONSerialization.dataWithJSONObject(...) to JSON NSData keep the AnyObject type.
0
func hitApi()
{

    var JSONarray: [Dictionary<String,String>] = []

    for (index,_) in reminderArray.enumerate()
    {
        JSONarray.append(["isAlarmOn":"true","time": reminderArray[index].time])
    }

    let bytes = try! NSJSONSerialization.dataWithJSONObject(JSONarray, options: NSJSONWritingOptions.PrettyPrinted)
    var jsonObj = try! NSJSONSerialization.JSONObjectWithData(bytes, options: .MutableLeaves) as! [Dictionary<String, String>]

    print("\"reminder\":\(jsonObj)")
}

You also want to change your Alarm Request Model to this:

class AlarmRequestModel {

    var time : String!

    init(time : String){
        if !time.isEmpty{
            self.time = time
        }

    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.