Swift Error: Consecutive statements on a line must be separated by ';' - ios

let email = MUser.sharedInstance.getUserEmail()
let json = [
"listIds": [""],
"contacts": [{ "email" : "\(email)" }]
];
I'm getting the error Consecutive statements on a line must be separated by ';' when running the code above. What am I doing wrong?

The problem is the dictionary email: email where {, } are not recognised keywords You can define your json like this :
let json = [
"""
"listIds": [""],
"contacts": [ {"email" : "\(email)" }]
"""
];
or if you prefer to construct your dictionary in contacts with code, you could do something like below:
let json = [
"listIds": [""],
"contacts": [[ "email" : "\(email)" ]]
];

Related

How to create parameter for request body for calling api if parameters required nested info to pass

I have to create request body for calling api which have nested dictionary formate to pass parameter. I am unable to create parameter
parameter should be in below format
let parameters: [[String: Any]] = [
"contributionIds":[
"ms.vss-tfs-web.project-members-data-provider-verticals"
],
"dataProviderContext": [
"properties": [
"sourcePage": [
"url":"https://xxx.visualstudio.com/abc%20abc",
"routeId":"ms.vss-tfs-web.project-overview-route",
"routeValues": [
"project":"abc",
"controller":"Apps",
"action":"ContributedHub",
"serviceHost":"2e1bc96a-9bac-4e6e-9e33-eb460123a138 (xxxxx)"
]
]
]
]
]
Above data need to post to api for get response from API
Getting an error
Contextual type '[[String : Any]]' cannot be used with dictionary literal
remove type notation [[String: Any]] because you are specifying wrong notation based on your structure type notation is [String: Any]. if you will not define type notation then that is fine as well.
Try Below Solution,
let parameters: [String: Any] = [
"contributionIds":[
"ms.vss-tfs-web.project-members-data-provider-verticals"
],
"dataProviderContext": [
"properties": [
"sourcePage": [
"url":"https://xxx.visualstudio.com/VSO%20Client",
"routeId":"ms.vss-tfs-web.project-overview-route",
"routeValues": [
"project":"VSO Client",
"controller":"Apps",
"action":"ContributedHub",
"serviceHost":"2e1bc96a-9bac-4e6e-9e33-eb460123a138 (xxxx)"
]
]
]
]
]
OR
let parameters = [
"contributionIds":[
"ms.vss-tfs-web.project-members-data-provider-verticals"
],
"dataProviderContext": [
"properties": [
"sourcePage": [
"url":"https://xxx.visualstudio.com/VSO%20Client",
"routeId":"ms.vss-tfs-web.project-overview-route",
"routeValues": [
"project":"VSO Client",
"controller":"Apps",
"action":"ContributedHub",
"serviceHost":"2e1bc96a-9bac-4e6e-9e33-eb460123a138 (xxxx)"
]
]
]
]
] as [String : Any]

Alamofire send Array of Dictionaries in a parameter

I have a POST API in which I send multiple parameters, one of those parameters has to be an array of dictionaries.
let arr = [
[
"id" : "1",
"price" : "10"
],
[
"id" : "2",
"price" : "20"
]
]
let params : Parameters = [
"param1" : "anyvalue1",
"param2" : "anyvalue2",
"param3" : arr,
]
When I use these parameters in Alamofire Request and hit the API, print(res.result.value) always returns unknown. Can anyone help me with this. Following is the way of requesting the API
Alamofire.request(url, method:.post, parameters: params).responseJSON{(res) in
print(res.result.value) //always shows 'unknown' as output
}
Try to make params a Dic of [String :Any ] like that :
let params : [String:Any] = [
"param1" : "anyvalue1",
"param2" : "anyvalue2",
"param3" : arr,
]

Recursive URL parameters construction

I have a NSDictionary parameters written like this :
{
"aKey" : [
{
"associated_values" : [
42,
43,
],
"internal_id" : 3
}
],
"another_Key" : false
}
Is there a simple way to construct a URL with is formatted this way for a GET method :
https://www.url.com/another_key=false?aKey[0][internal_id]=3&aKey[0][associated_values][0]=42&aKey[0][associated_values][1]=43
We can imagine that associated_values can also contains subjson

SwiftyJSON how to append data

I try to create JSON with this structure:
var json: JSON = [
"params": [
"token": Utilities.token,
"language": "RU",
"billerId": biller.id,
],
"data": [
"serviceData": [
//I want put here additional data
]
]
]
in "serviceData" I want to add fields and values, but I don't know how much them and what is his name before compiling.
I try add this fields by this way:
for item in templateItems{
let key:String = item.name
let value: String = item.value
json["data"]["serviceData"][key] = value
}
according to https://github.com/SwiftyJSON/SwiftyJSON/tree/a1356035d2de68c155d05521292f0609ef7e69bb#literal-convertibles
but it doesn't work
It shouldn't be array, it is key-value dictionary.
Replace
"seviceData": []
With
"serviceData": [:]
The former defines an empty array. The latter defines an empty dictionary.

json parsing in swift

Here is my Json
{
"id": "63",
"name": "Magnet",
"price": "₹1250",
"description": "",
"image": [
"catalog/IMG-20150119-WA0012_azw1e3ge.jpg",
"catalog/IMG-20150119-WA0029_6mr3ndda.jpg",
"catalog/IMG-20150119-WA0028_ooc2ea52.jpg",
"catalog/IMG-20150119-WA0026_4wjz5882.jpg",
"catalog/IMG-20150119-WA0024_e38xvczi.jpg",
"catalog/IMG-20150119-WA0020_vyzhfkvf.jpg",
"catalog/IMG-20150119-WA0018_u686bmde.jpg",
"catalog/IMG-20150119-WA0016_c8ffp19i.jpg"
],
"thumb_image": [
"cache/catalog/IMG-20150119-WA0012_azw1e3ge-300x412.jpg",
"cache/catalog/IMG-20150119-WA0029_6mr3ndda-300x412.jpg",
"cache/catalog/IMG-20150119-WA0028_ooc2ea52-300x412.jpg",
"cache/catalog/IMG-20150119-WA0026_4wjz5882-300x412.jpg",
"cache/catalog/IMG-20150119-WA0024_e38xvczi-300x412.jpg",
"cache/catalog/IMG-20150119-WA0020_vyzhfkvf-300x412.jpg",
"cache/catalog/IMG-20150119-WA0018_u686bmde-300x412.jpg",
"cache/catalog/IMG-20150119-WA0016_c8ffp19i-300x412.jpg"
],
"specifications": [
{
"Fabrics": [
"Pure chiffon straight cut suits 48" length"
]
},
{
"MOQ": [
"Minimum 10"
]
}
]
}
In above json string the "specification" arraylist has dynamic number of key and each key has dynamic number of values
So how can parse this? Please help if anyone knows this...
Thanks in advance
There are multiple ways to do parsing. In your case specifications should be an array, so you'll be able to loop on each items.
You might want to :
Create your own JSON parse class / methods ;
Use an existing library to parse JSON.
For the second option, you can give a look at the following :
https://github.com/Wolg/awesome-swift#jsonxml-manipulation
var yourJson = data as? NSDictionary
if let id = yourJson.valueForKey("id") as String
{
//save your id from json
}
if let name = yourJson.valueForKey("name") as String
{
//save your name
}
...
if let images = yourJson.valueForKey("image") as NSArray
{
for im in images
{
//save image
}
//the same for all othe images
}
... And so on...
You should also watch some tutorials, to understand the basics of JSON parsing..
https://www.youtube.com/watch?v=MtcscjMxxq4

Resources