Sending POST request to API using Alamofire - arrays

So Im using an API for an app that helps me store data for events where i send data to the API entered/selected by the user store it and get back an ID to reference to later.
Here is some code that Im using.
JSON accepted by API
{
"topic":"test shedule",
"setdate":"21-09-2017",
"scheduledate":"22-09-2017",
"settime":"09:00:00",
"scheduletime":"10:00:00",
"agenda":"hi this is test agenda",
"adminname":"name",
"type":"meeting",
"users": "[{\"category\":\"optional\",\"email\":\"abc#gmail.com\"}]",
"adminemail":"abc#gmail.com"
}
Data Im Sending using Alamofire\
let parameters: Parameters = [
"topic":topicTextField.text!,
"setdate":currentDate as String,
"scheduledate":setDate as String,
"settime":currentTime as String,
"scheduletime":setTime as String,
"agenda":agendaTextField.text!,
"adminname":"abcname",
"type":"meeting",
"users" : "\(smembers)",
"adminemail":"abc#gmail.com"
]
Alamofire.request("\(baseURL)meeting_shedule.php",method: .post, parameters: parameters).responseJSON { response in
if let dict = response.result.value as? Dictionary<String, AnyObject> {
let topic = dict["topic"] as? String
let g_id = dict["g_id"] as? String
self.storeMeeting(topic: topic!, g_id: g_id!)
}
}
smembers is an array created from the dictionary selectedMembers and the dictionary selectedMembers comes from a different ViewController by using prepare for segue
var selectedMembers = [String: String]()
var smembers = [Any]()
var selected = [String: String]()
if selectedMembers.isEmpty == false {
for (key, value) in selectedMembers {
selected = ["email":key, "category":value]
smembers.append(selected)
}
}
The Problem
Im facing a failure response from api.
i think the problem is sending "users" to the API as the API is asking for data in string form and Im sending an array i have even tried to solve this problem by converting smembers to jsonstring by serialization but still returns failure.
Image of error Im receiving
The api works when Im sending request through post man like this.
I have no clue how to approach this problem, how to send the data that the API will accept.
Thanks for your help

Change logic in as shown bellow
var selectedMembers = [String: String]()
var smembers = [String]()
var selected = ""
if selectedMembers.isEmpty == false {
for (key, value) in selectedMembers {
selected = String(format: "{\"\\email\\\":\"\\%#\"\\,\"\\category\"\\:\"\\%#\"\\}", arguments: [key,value])
smembers.append(selected)
}
}
Then in ur post data
let parameters: Parameters = [
"topic":topicTextField.text!,
"setdate":currentDate as String,
"scheduledate":setDate as String,
"settime":currentTime as String,
"scheduletime":setTime as String,
"agenda":agendaTextField.text!,
"adminname":"abcname",
"type":"meeting",
"users" :"[\(smembers.joined(separator: ","))]",
"adminemail":"abc#gmail.com"
]

Related

Convert JSON to Array Xcode

I am trying to implement two side by side UIPickerViews, one for Type and the other for Subtype. Each is an array of strings
var typePickerData: [String] = [String]()
var subtypePickerData: [String] = [String]()
Each is a pretty simple array of names:
typePickerData = ["Monitor","Keyboard","Mouse"]
When the viewDidLoad fires or when a new Type is selected, the app makes a remote db call and I get a response containing JSON of subtype names which I want to use to populate and reload the subtype picker.
I am stuck on converting the response into subtypePickerData
let decoder = JSONDecoder()
if let jsonResponse = try? decoder.decode([String].self, from: data) {
print("parse subtype response \(jsonResponse)")
subtypePickerData = jsonResponse
DispatchQueue.main.async {
self.subtypePicker.reloadAllComponents()
}
}
What am I doing wrong here converting the JSON response to subtypePickerData?
this is what I am getting from the remote call
result Optional(<__NSArrayM 0x281370e70>( { name = Monitor; },{ name = "AV Accessories"; },{ name = Computer; },{ name = "Monitor Stands"; },{ name = "Bracket/Mount"; },{ name = Screen; }
Here is my updated code after solving issue
let decoder = JSONDecoder()
if let jsonResponse = try? decoder.decode(Subtypes.self, from: data) {
SubtypeList = jsonResponse.result
self.subtypePickerData = SubtypeList.map{$0.Name}
DispatchQueue.main.async {
self.subtypePicker.reloadAllComponents()
}
}
Yor response seems to be not type of [String] but an array of custom objects. You first need to create a struct to decode your response data to.
struct NameContainer{
var name: String
}
then do:
//change decoding to String array to decoding array of custom object NameContainer
if let jsonResponse = try? decoder.decode([NameContainer].self, from: data) {
print("parse subtype response \(jsonResponse)")
subtypePickerData = jsonResponse.map{$0.name} // Map your objects to strings and assign them
DispatchQueue.main.async {
self.subtypePicker.reloadAllComponents()
}
}
Remarks:
Never use try? this will obfuscate all errors. Use a proper do/catch block, handle the error or mark your function throws and handle the error up the chain.
DispatchQueue.main.async {
self.subtypePicker.reloadAllComponents()
Bracket/Mount"; },{ name = Screen; } –

How can i add specific JSON value to another array in Swift?

I'm new in IOS programming.
I have a json array described with code below.
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as?
NSDictionary
print("json: \(String(describing: json))")
Output of code is;
json: Optional({
vendors = (
{
firm = "XXX firm";
id = 1;
"show_firm" = 1;
},
{
firm = "ZZZZZ firm";
id = 2;
"show_firm" = 1;
}
);
})
I want to add only firm values to another array like firms = ["XXX firm" , "ZZZZZ firm"]
How can I do that?
Any help would be greatly appreciated.
#witek bobrowski asked String(data: data!, encoding: .utf8) output.This output is below also. By the way json data comes from server as http post response.
json2: Optional("{\"vendors\":[{\"id\":\"1\",\"firm\":\"XXX firm\",\"show_firm\":\"1\"},{\"id\":\"2\",\"firm\":\"ZZZZZ firm\",\"show_firm\":\"1\"}]}")
I believe the best way to go is to decode the JSON and then add the firms value to an array.
struct model: Decodable{
var vendors: [decodingModel]
}
struct decodingModel: Decodable{
var firm: String
var id: Int
var show_firm: Int
}
let decoder = JSONDecoder()
do{
let result = try decoder.decode(model.self, from: jsonData)
let firmsArray = result.vendors.compactMap({$0.firm})
}catch{
print(error)
}
Since you have not posted your son structure, I can only assume you have a Json where vendor is an array of jsons. firmsArray is what you are looking for.
If this doesn't work is probably because of the wrong model and decodingModel. If you post your json structure, I will update the code so that you can properly decode your json
the best way is to create Decodable Model for your json as below:
struct Firm: Decodable {
let id: Int
let name: String
let showFirm: Int
enum CodingKeys: String, CodingKey {
case id
case name = "firm"
case showFirm = "show_firm"
}
}
I created this factory method to simulate your json response locally based on what you provided in the question
struct FirmFactory {
static func makeFirms() -> [Firm]? {
let json = [
[
"firm": "XXX firm",
"id": 1,
"show_firm": 1,
],
[
"firm": "ZZZZZ firm",
"id": 2,
"show_firm": 1,
],
]
// you should use the following code to decode and parse your real json response
do {
let data = try JSONSerialization.data(
withJSONObject: json,
options: .prettyPrinted
)
return try JSONDecoder().decode([Firm].self, from: data)
} catch {
print("error \(error.localizedDescription)")
return nil
}
}
}
now you will be able to map only the firm names as you request you can test like this
let firmNames = FirmFactory.makeFirms()?.map { $0.name }
print("firmNames \(firmNames)")
I answered my own question again. There are 2 answers given but i didn't use any of these in my code. May be these two answers are usable but because of i'm new in IOS i couldn't use any of them. At the end of long google search i solved my problem as below.
let vendors = json!["vendors"]! as! [[String : AnyObject]]
for firm in vendors {
let firm1 = firm["firm"]! as! String
self.providerArray.append(firm1)
}
I hope this answer solves someone else's problem like me.

How to take a function result and turn it into a key value pair array

I am fairly new to Swift but I have a function that returns a key value pair result of numbers
func dataPostRequest(_ url:String,_ paramString:String)
{
let url:NSURL = NSURL(string: url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest) {
(
data, response, error) in
guard let _:NSData = data as NSData?, let _:URLResponse = response, error == nil else {
print("error")
return
}
if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
{
print(dataString)
}
}
task.resume()
}
I am able to call the function like this:
dataPostRequest("https://myserver.com/","user_email=emailtest.com")
This works correctly but I want to now use the data that I've pulled and display it in a Table View. I have my tableView set up correctly but I am wondering how I can take my function and turn it into a key value pair array or a dictionary that I can use. I have tried creating an empty dictionary and set my function call to it but I get an error:
var emptyDictionary = [Int: Int]()
emptyDictionary = dataPostRequest("https://myserver.com/","user_email=emailtest.com")
And no matter what variation I try I keep getting:
Cannot assign value of type '()' to type '[Int : Int]'
Any guidance would be greatly appreciated.
dataPostRequest has no return value (aka ()). You can decode the received data in the completion handler and assign it to the dictionary. See the comment line below.
If you need to proceed in another function you have to add a completion handler described here.
Basically don't use NS... classes in Swift at all if there are native counterparts. And don't annotate types the compiler can infer.
The native Swift version is
func dataPostRequest(with url:String, paramString : String)
{
let url = URL(string: url)!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = paramString.data(using: .utf8)
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
print(error)
return
}
let dataString = String(data: data!, encoding: .utf8)!
print(dataString)
// here decode the data to the desired type and assign it to emptyDictionary
}
task.resume()
}

Accessing Array outside of the loop [duplicate]

This question already has an answer here:
Assign value of a Firestore document to a variable
(1 answer)
Closed 2 years ago.
I have an Array where some different UserID's are stored.
Each UserID is connected with their corresponding data in Firestore.
So now I want to fetch all the JSON Data from the UserID's and make them accessible to decode them later.
Therefore the fetched Data (coming as a Dictionary) from each user must be accessible separately.
I tried it with that way:
var fetchedIDs = ["ID1", "ID2", "ID3"]
var finalArray = [[String: Any]]()
for id in fetchedIDs{
let docRef = Firestore.firestore().collection("Data").document(id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let myDict: [String: Any] = document.data() as! [String: Any]
finalArray.append(myDict)
}
}
}
}
But the problem is that the finalArray (like finalArray[0]) is just accessible in the For-Loop.
It should be accessible outside of the loop and look like that:
finalArray[0] should have the Dictionary data from ID1
finalArray[1] should have the Dictionary data from ID2
Maybe I am thinking to complicated for that..
Can someone help me out ?
Is there generally a good source for learning about scopes and how the data should be accessed like in that scenario?
Thanks!
Finally get it working with the following code:
var fetchedIDs = ["ID1", "ID2", "ID3"]
func loadData(com:#escaping( ([[String: Any]]) -> ())){
var myArray = [[String: Any]]()
for id in fetchedIDs{
let refLike = db.collection("Data").document(id)
refLike.getDocument { (document, error) in
if let err = error {
print("Error getting documents: \(err)")
} else {
let myDict: [String: Any] = document?.data() as! [String: Any]
myArray.append(myDict)
}
com(myArray)
}
}
}
loadData(){ arr in
if (arr.count == fetchedIDs.count){
print ("User 1 has the following data: \(arr[0])")
print ("User 2 has the following data: \(arr[1])")
}
}

why are my items not going into the array? Xcode and swift NSURLSession

I am using Swift and Xcode, I have built model object with the following variables:
var itemImageNames: [String]?
var itemTitle: String?
var itemDescription: String?
var itemURL: String?
In the mainviewcontroller, I created an variable of model type. I am initiating a NSURLSession...dataTaskWithURL... and adding itemImageNames that I receive back from the server by using append. The data comes back as valid, I've parsed it and it is indeed coming back as Strings. I've tried two solutions,
create a string array out of the images and set that array to self.item.itemImageNames?
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String: AnyObject]] {
var imageURLs: [String] = [""]
for dictionary in json {
if let imageURL = dictionary["url"] as? String {
imageURLs.append(imageURL)
print(imageURL)
}
}
self.featuredItem.itemImageNames? = imageURLs
append each of the strings as I get them using self.item.itemImageNames?.append(image)
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String: AnyObject]] {
for dictionary in json {
if let imageURL = dictionary["url"] as? String {
self.featuredItem.itemImageNames?.append(imageURL)
print(imageURL)
}
}
For some reason, the itemImageNames remains nil, using both approaches. I am sure it will work if I just use one image (e.g. the 1st image), and change itemImageNames to a "String?".
In addition, I can update the itemTitle, itemDescription, and itemURL easily by just setting them to self.item.itemTitle, self.item.itemDescription, self.item.itemURL, respectively. Is there something I'm missing on how to enter information into an array?
In approach #2 initialize the itemImageNames array before trying to append to it. If you try to append to an array that is nil then nothing will happen.
itemImageNames = []
for dictionary in json {
if let imageURL = dictionary["url"] as? String {
self.featuredItem.itemImageNames?.append(imageURL)
print(imageURL)
}
}

Resources