To create key value pairs according to user input in an array in swift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



To create key value pairs according to user input in an array in swift



I need to create an array of key-value pairs to a key "availabilities" according to user inputs in the following format to post the data. Confused about how to do that.


"availabilities"


"availabilities": [

"availabilites_start_time": "6:00",
"availabilites_end_time": "10:00",
"availabilites_dayofweek": "1.",
"availabilites_shift": "Morning"

],





what you have tried?
– iVarun
Aug 10 at 4:25






Does my answer works for you?
– iVarun
Aug 10 at 6:40




3 Answers
3



Hope this will helps:


var availabilitiesDict = [String: Any]()
availabilitiesDict["availabilities"] = [["availabilites_start_time" : "6:00", "availabilites_end_time": "10:00", "availabilites_dayofweek" : "1.", "availabilites_shift" : "Morning"]]

let jsonData = try? JSONSerialization.data(withJSONObject: availabilitiesDict, options: )
let jsonString = String(data: jsonData!, encoding: .utf8)!
print(jsonString)



enter image description here



First of all create a Struct as a data model and one array as data source as shown in below code.


struct Availabilities
let availabilites_start_time, availabilites_end_time, availabilites_dayofweek, availabilites_shift : String


var availabilitiesArray = [Availabilities]()



We get memberwise initializers for free, so whenever user Picks his choices create an instance of Availabilities with those values as shown in below code snippet.


let availability = Availabilities (availabilites_start_time: "6:00", availabilites_end_time: "10:00", availabilites_dayofweek: "1", availabilites_shift:"Morning" )



Since you have a codable type struct, you can use JSONEncoder to convert your type to Data which can be either written to a file or sent over the network.


let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(availability)

let jsonString = String(data: jsonData, encoding: .utf8)
print(jsonString)



I will suggest 1st create the inside array of dictionary then outside dictionary.


var myData = [String:Any]()
var dataArray = [[String:Any]]()
var finalDict = [String : Any]()

myData["availabilites_start_time"] = "6:00"
myData["availabilites_end_time"] = "10:00"
myData["availabilites_dayofweek"] = "1."
myData["availabilites_shift"] = "Morning"

dataArray.append(myData)
finalDict["availabilities"] = dataArray

print(finalDict)

let jsonData = try? JSONSerialization.data(withJSONObject: finalDict, options: )
let jsonString = String(data: jsonData!, encoding: .utf8)!
print(jsonString)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS