Swift: dictionaries inside array - swift

Data:
[
{ firstName: "Foo", lastName: "Bar" },
{ firstName: "John", lastName: "Doe" }
]
How can I have this kind of structure using swift array and dictionary? This data shows dictionaries inside an array, right? So I suggest:
var persons:Array = [Dictionary<String, String>()]
but this gives me the error:
Cannot convert the expressions type () to type Array<T>
Any ideas?

The correct way is:
var persons = [Dictionary<String, String>]()
which is equivalent to:
var persons = [[String : String]]()
What your code does instead is to create an array filled in with an instance of Dictionary<String, String>, whereas I presume you want an empty instance of the array containing elements of Dictionary<String, String> type.

Which version of Xcode have you got?
Your code should work fine but the line:
var persons:Array = [Dictionary<String, String>()]
create the array with first empty dictionary, try this instead:
var persons:Array = [Dictionary<String, String>]()
var dic1 = ["Name" : "Jon"]
var dic2 = ["Surname" : "Smith"]
persons.append(dic1)
persons.append(dic2)
println(persons)

Are you sure you really want a dictionary within an array? The code you've given indicates more an array with named columns, which can be achieved using something like the following:
struct Name {
var firstName : String
var lastName : String
}
var persons1 : Array<Name> = [
Name(firstName: "Foo", lastName: "Bar"),
Name(firstName: "John", lastName: "Doe")
]
persons1[0].firstName // "Foo"
var persons2 : Array<(firstName: String, lastName:String)> = [
(firstName: "Mary", lastName: "Mean"),
(firstName: "Foo", lastName: "Bar"),
(firstName: "John", lastName: "Doe")
]
persons2[1].firstName // "Bar"
These are proper arrays and adressed as such using subscripts. The dictionary type is usually a combination of key and value, i.e. nickname as key, and name as value.
var nickNames : [String:String] = [
"mame" : "Mary Mean",
"foba" : "Foo Bar",
"jodo" : "John Doe"]
nickNames["mame"]! // "Mary Mean"
And here we lookup on the key value, and get an optional value in return, which I forcefully unwrapped...
All of these can be appended rather easily, but do note that the named tuple variant, persons2, is not following recommended practice. Also note that the Array of Dictionaries allow for inclusion on different keys as suggested in my last injection.
persons1.append( Name(firstName: "Some", lastName: "Guy") )
persons2.append( firstName: "Another", lastName: "Girl" )
nickNames["anna"] = "Ann Nabel"
// Array of Dictionaries
var persons : [[String:String]] = [
[ "firstName" : "Firstly", "lastName" : "Lastly"],
[ "firstName" : "Donald", "lastName" : "Duck"]
]
persons.append( ["firstName" : "Georg", "middleName" : "Friedrich", "lastName" : "Händel"] )

something like this can work for you:
var persons: Array<Dictionary<String, String>> = Array()
and now you can add the names:
persons.append(["firstName": "Foo", "lastName": "Bar"]);
persons.append(["firstName": "John", "lastName": "Doo"]);
NOTE: if you are insecure how to use literals, just don't use them.

Related

URLSession POST request with json "_id" element with codable class in Swift

I'm trying to use URLSessions to get and post data. I don't have problems with get requests on Swift, just post. I followed this
I have a codable class that looks like this:
class Item: Codable {
var _id: String?
var name: String = ""
var color: String = ""
var rating: Int = 0
init(name: String, color: String, rating: Int){
self.name = name
self.color = color
self.rating = rating
}
}
And json data that looks like this:
[ {
"_id" : "5e50a10c4ea5d87f0001c9da",
"name" : "Pepper",
"color" : "blue",
"rating" : 4
},
{
"_id" : "5e50a10c4ea5d87f0001c9db",
"name" : "Pepper",
"color" : "blue",
"rating" : 2
},
{
"_id" : "5e50a10c4ea5d87f0001c9dc",
"name" : "Pepper"
"color" : "blue"
"rating" : 6
}
]
I'm currently using restdb.io for my database, and have tested my requests using Postman for all Get,Post,Put,...etc.
On Postman, when I create a POST request with Json body with just name, color and rating elements, it will generate a unique _id without me having to specify.
When I do this on Swift and send a post request with Item object using the init() method, where I left _id as an optional in the class, my code crashes with an "Unexpectedly found nil while implicitly unwrapping an Optional value". How do I work around that?
If you don't need _id in your swift code you can ignore it and not include it in your codable struct. In this way, the codable will ignore it and not process it

Unable to create an array of JSON's and assign it to a key which are to be send as parameters to Alamofire Post request?

I have a Post Request, in which I am trying to create an Array of json which the user types and then send to the server, I have used dictionary and it is working for a single request but not for multiple requests.
The JSON structure to be sent is
{
"id" : "u_101"
"data" : [
{ "name" : "Shubham"
"age" : "23"
},
{
"name" : "S"
"age" : "20"
}
]
}
Here is what I am using in swift for setting the parameters of alamofire request.
func setData (id: String, data: [Any]) {
request.httpMethod = post
var parameters = Parameters()
parameters["id"] = id
parameters["data"] = data
}
Then in the view controller I am doing this, (Items contain a dictionary of entered data through the view )
var allData : [Any] = []
for item in items {
var data: [String:String] = [:]
data["name"] = item.key
data["age"] = item.value
allData.append(data)
}
setData(id: "u_101", data: alldata)
This is not working and the server is throwing error.
If I send this to the Alamofire post request.
{
"id" : "u_101"
"data" : [
{ "name" : "Shubham"
"age" : "23"
}
]
}
The server responds with a success.

How parse json recursively using codable with swift

I am trying to defining a decoding class model to decode this kind of json file:
Here a short extraction to understand the problem, in reality it is more nested.
{
"Title" : "Root",
"Subtitle" : "RootTree",
"Launch" : [
{
"DisplayName" : "Clients",
"Launch" : [
{
"DisplayName" : "Clients Details",
"Launch" : [
{
"DisplayName" : "Item1",
"URI" : "/rest/..."
},
{
"DisplayName" : "Item2",
"URI" : "/rest/..."
},
{
"DisplayName" : "Item3",
"URI" : "/rest/..."
}
]
}
]
}
]
}
Here my structure, I used a class because of the recursive usage:
final class Url: Codable {
let name : String
let uri: String?
let launch: [LaunchStructure]?
enum CodingKeys: String, CodingKey {
case name = "DisplayName"
case uri = "URI"
case launch = "Launch"
}
}
final class LaunchStructure: Codable {
let launch: [Url]
enum CodingKeys: String, CodingKey {
case launch = "Launch"
}
}
In the Title and Subtitle I am not interested, therefore I have excluded it from the class. I would like to get the Displayname and the uri from the items. As I said, the structure is more nested, but always the same structure. Is it possible to read the elements using a recursive way.
I am to decode it in this manner:
...
let result = Result { try JSONDecoder().decode(LaunchStructure.self, from: data) }
Thank you, best regards
Arnold
You don't need two types here at all, just one will do:
struct Item: Codable {
let name : String? // not all entries in your example has it, so it's optional
let uri: String?
let launch: [Item]? // same here, all leaf items doesn't have it
enum CodingKeys: String, CodingKey {
case name = "DisplayName"
case uri = "URI"
case launch = "Launch"
}
}

Dictionary wrong order - JSON

I am trying to create a dictionary that I can make into a JSON formatted object and send to the server.
Example:
var users = [
[
"First": "Albert",
"Last": "Einstein",
"Address":[
"Street": "112 Mercer Street",
"City": "Princeton"]
],
[
"First": "Marie",
"Last": "Curie",
"Address":[
"Street": "108 boulevard Kellermann",
"City": "Paris"]]
]
I use this function
func nsobjectToJSON(swiftObject: NSObject) -> NSString {
var jsonCreationError: NSError?
let jsonData: NSData = NSJSONSerialization.dataWithJSONObject(swiftObject, options: NSJSONWritingOptions.PrettyPrinted, error: &jsonCreationError)!
var strJSON = NSString()
if jsonCreationError != nil {
println("Errors: \(jsonCreationError)")
}
else {
// everything is fine and we have our json stored as an NSData object. We can convert into NSString
strJSON = NSString(data: jsonData, encoding: NSUTF8StringEncoding)!
println("\(strJSON)")
}
return strJSON
}
But my result is this:
[
{
"First" : "Albert",
"Address" : {
"Street" : "112 Mercer Street",
"City" : "Princeton"
},
"Last" : "Einstein"
},
{
"First" : "Marie",
"Address" : {
"Street" : "108 boulevard Kellermann",
"City" : "Paris"
},
"Last" : "Curie"
}
]
Problem: why is the last name last? I think it should be above address. Please let me know what I am doing wrong with the NSDictionary for this to come out wrong. Any help would be very much appreciated - thank you.
To post what has already been said in comments: Dictionaries are "unordered collections". They do not have any order at all to their key/value pairs. Period.
If you want an ordered collection, use something other than a dictionary. (an array of single-item dictionaries is one way to do it.) You can also write code that loads a dictionary's keys into a mutable array, sorts the array, then uses the sorted array of keys to fetch key/value pairs in the desired order.
You could also create your own collection type that uses strings as indexes and keeps the items in sorted order. Swift makes that straightforward, although it would be computationally expensive.
I did like this.
let stagesDict = NSDictionary()
if let strVal = sleepItemDict["stages"] as? NSDictionary {
stagesDict = strVal
let sortedKeys = (stagesDict.allKeys as! [String]).sorted(by: <)
var sortedValues : [Int] = []
for key in sortedKeys {
let value = stagesDict[key]!
print("\(key): \(value)")
sortedValues.append(value as! Int)
}
}

How do I use JSON arrays with Alamofire parameters?

I'm having a bit of trouble structuring my parameters so that our server API would be able to read it as valid JSON.
Alamofire uses parameters like this in swift language
let parameters : [String: AnyObject] =
[
"string": str
"params": HOW I INSERT A VALID JSON ARRAY HERE
]
The problem is that AnyObject does not seem to accept JSON so how would I send / create a structure like this with swift?
{
"string": str, "params" : [
{
"param1" : "something",
"param2" : 1,
"param3" : 2,
"param" : false
},
{
"param1" : "something",
"param2" : 1,
"param3" : 2,
"param" : false
}]
}
Taken from Alamofire's GitHub page:
let parameters = [
"foo": [1,2,3],
"bar": [
"baz": "qux"
]
]
Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters, encoding: .JSON)
// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
EDIT: And from your example:
let parameters = [
"string": "str",
"params": [[
"param1" : "something",
"param2" : 1,
"param3" : 2,
"param" : false
],[
"param1" : "something",
"param2" : 1,
"param3" : 2,
"param" : false
]
]
]
Solved this myself. I can just do
parameters =
[
"params": array
]
Where array is Dictionary (String, AnyObject). The problem I initially had with this solution was that you can't insert booleans into this kind of dictionary, they will just be converted into integers. But apparently alamofire JSON encoding (I think) sends them as true/false values nevertheless.
In case, there is a need to pass array directly as a parameter for a alamofire request, the following method worked for me.
source: https://github.com/Alamofire/Alamofire/issues/1508
let headers = NetworkManager.sharedInstance.headers
var urlRequest = URLRequest(url: URL(string: (ServerURL + Api))!)
urlRequest.httpMethod = "post"
urlRequest.allHTTPHeaderFields = headers
let jsonArrayencoding = JSONDocumentArrayEncoding(array: documents)
let jsonAryEncodedRequest = try? jsonArrayencoding.encode(urlRequest, with: nil)
var request: Alamofire.DataRequest? = customAlamofireManager.request(jsonAryEncodedRequest!)
request?.validate{request, response, data in
return .success
}
.responseJSON {
You need to create a NSArray object for array parameters.
var yourParameters = [
"String": "a string",
"Int": 1,
"Array": NSArray(
array: [
"a", "b", "c"
])
]
Swift 2.2 and using SwiftyJSON.swift
You can use like this.
var arrayList : [String: AnyObject]//one item of array
var list: [JSON] = []//data array
for i in 0..<10//loop if you need
{
arrayList = [
"param1":"",
"param1":"",
"param2":["","",""]
]
list.append(JSON(arrayList))//append to your list
}
//params
let params: [String : AnyObject]=[
"Id":"3456291",
"List":"\(list)"//set
]
if you are using SwiftyJSON, you can write like this
let array = ["2010-12-13T5:03:20","2010-12-13T5:03:20"]
let paramsJSON = JSON(array)
var arrString = paramsJSON.rawString(NSUTF8StringEncoding)
Using Swift 5
You can use like this.
var authParams:[String:Any] = [:]
var authParamsObject:[String:Any] = [:]
authParamsObject["is_urgent"] = data_any
authParamsObject["body"] = data_any
authParams = ["note_set" : authParamsObject]
Json Result :
{
"note_set":
{
"is_urgent": true,
"body": "string"
}
}