geting error with Dictionary - ios

let parameter : Dictionary<String,AnyObject> = ["action":"add-playlist-item","playlist_id":self.dictPlayList.objectForKey("ID")!,"kod_id":arrayOfID]
error
["action": add-playlist-item, "playlist_id": 166, "kod_id": <_TtCs21_SwiftDeferredNSArray 0x7c615620>(
21,
18
)
]
_TtCs21_SwiftDeferredNSArray 0x7c615620 what does this error mean??

Array is a value type and it's not an object, but a struct. So it doesn't conform to AnyObject protocol. Use Any instead of AnyObject. See more details here and here.

Check that you are using JSON parameter encoding, not URL encoding.
You may be having the same problem I was having. I was seeing <_TtCs21_SwiftDeferredNSArray 0x7c615620> appear in my requests too. When I changed to JSON encoding, everything worked. I don't think a dictionary structure like that can be properly URL encoded. I'm not sure how you're actually making the request or I would post code.

Related

Array.size() returned wrong values (Grails)

I'm developing an app using Grails. I want to get length of array.
I got a wrong value. Here is my code,
def Medias = params.medias
println params.medias // I got [37, 40]
println params.medias.size() // I got 7 but it should be 2
What I did wrong ?
Thanks for help.
What is params.medias (where is it being set)?
If Grials is treating it as a string, then using size() will return the length of the string, rather than an array.
Does:
println params.medias.length
also return 7?
You can check what Grails thinks an object is by using the assert keyword.
If it is indeed a string, you can try the following code to convert it into an array:
def mediasArray = Eval.me(params.medias)
println mediasArray.size()
The downside of this is that Eval presents the possibility of unwanted code execution if the params.medias is provided by an end user, or can be maliciously modified outside of your compiled code.
A good snippet on the "evil (or lack thereof) of eval" is here if you're interested (not mine):
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
I think 7 is result of length of the string : "[37,40]"
Seems your media variable is an array not a collection
Try : params.medias.length
Thanks to everyone. I've found my mistake
First of all, I sent an array from client and my params.medias returned null,so I converted it to string but it is a wrong way.
Finally, I sent and array from client as array and in the grails, I got a params by
params."medias[]"
List medias = params.list('medias')
Documentation: http://grails.github.io/grails-doc/latest/guide/single.html#typeConverters

how to avoid Alamofire change parameters automatically?

It's strange when i see in Xcode console, the output of URL parameters are not the same as i inputted using Alamofire,
parameters' sequence changed
and extra numbers "25" added into parameter "key",
Pls see following original code:
let weatherURL="http://open.weather.com.cn/data/"
let params=["areaid":"\(areaid)","type":"forecast_v","date":"\(time)","appid":"1eb583","key":"\(URLEncodeKey)"]
Alamofire.request(.GET, weatherURL, parameters: params)
.responseJSON { (request, response, json, error) in
if((error) != nil){
println(request)
println("Error:\(error)")
}else{
println(request)
println(json)
}
}
the output of URL in console:
key:2v7eK8AlzynX%2BuLBgw7DU74f8S0%3D
NSMutableURLRequest: 0x7fe3f14a1bf0 { URL: http://open.weather.com.cn/data/?appid=1eb583&areaid=101020900&date=201507211626&key=2v7eK8AlzynX%252BuLBgw7DU74f8S0%253D&type=forecast_v }
These two problems caused error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.)
Thus i can't get the data from API. But when the URL is corrected from above two problems by hand, means URL:
http://open.weather.com.cn/data/?areaid=101020900&type=forecast_v&date=201507211626&appid=1eb583&key=2v7eK8AlzynX%2BuLBgw7DU74f8S0%3D
change the parameters sequence as the same in constant "params",
and remove extra "25".
Then it works, i can see the responding data from API in web browser.
So, please, what problems i ignored in my code?
Thank you!
You say that you are seeing the following on the console:
key:2v7eK8AlzynX%2BuLBgw7DU74f8S0%3D
You're not showing us how this key value was generated, but that's very strange, because that string is percent escaped, and it shouldn't be. If you remove the percent escapes, you see something like
key:2v7eK8AlzynX+uLBgw7DU74f8S0=
And that is a well formed base64 string.
The thing is, if you're using Alamofire, you should not be percent escaping it (Alamofire does that for you). In fact, that's why you're seeing the extra "25", because it's percent escaping your key string a second time, replacing the % characters with %25.
Bottom line, find out why the key is already percent escaped, and prevent that from happening. (Or, remove the percent escapes with stringByReplacingPercentEscapesUsingEncoding before adding it to the dictionary; but it's better to prevent the percent escaping rather than adding and then replacing the percent escapes.) If you pass it the 2v7eK8AlzynX+uLBgw7DU74f8S0= value in the parameters dictionary, everything should be fine.
Regarding of the parameters collection: The parameters is a dictionary, and unlike arrays, dictionaries are not ordered collections, and they won't preserve the order you specify the keys. Fortunately, the parameters in HTTP requests in standard web servers are not order-specific, either, so this is not an issue.

RestKit path pattern for comma separated dynamic arguments

I am using RestKit, i have send a single GET request to get a bulk data to a URL like this
api/exemptions?ids=203,1985,21855
What path pattern can be set for this in RestKit response descriptor?
I know for predefined number of dynamic argument we can use something like this #"/api/sms/confirmation/:arg1/:arg2"
but above mentioned case is new for me.
EDIT
I found that parameter argument in
[[RKObjectManager sharedManager] getObjectsAtPath:path parameters:nil
will do the job, but it requires a dictionary so i am giving it an example dictionary NSDictionary *args = #{ #"ids" : #[#"1",#"2",#"3",#"4"] };
when executed this encoded url is generated
http://../api/exemptions?&ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3&ids%5B%5D=4
"ids" key is repeating, what is going wrong here.
EDIT # 2
URL encoding problem is solved, but the main problem still persists, path pattern is not matching on response, I am using this path pattern currently
pathPattern:#"/api/exemptions?&ids"
for this url /api/exemptions?ids=203,1985,21855
i have also tried pathPattern:#"/api/exemptions?&ids="
Please help, This is becoming huge pain.
Based on your sample code and response, have you tried:
NSDictionary *args = #{ #"ids": [#[#"1", #"2"] componentsJoinedByString: #","] };
This looks like it would encode with the desired value, since the joining leads to a dictionary value of #{ #"ids": #"1,2" }.

JSON Parsing issue in Rails

I am seeing a strange issue in Rails.
Request Body (request.body):
renewals[][driver_1][dl_number]=123&
renewals[][driver_1][expiration_date]=20130513&
renewals[][driver_1][last_name]=123&
renewals[][driver_1][state]=AL&
renewals[][driver_1][verified]=1&
renewals[][driver_2][verified]=0&
renewals[][id]=6415&
renewals[][insurance][expiration_date]=20130513&
renewals[][insurance][naic]=123&
renewals[][insurance][policy_number]=123&
renewals[][insurance][verified]=1&
renewals[][mailing_address][address_has_changed]=0&
renewals[][mailing_address][city]=GULF%20SHORES&
renewals[][mailing_address][state]=AL&
renewals[][mailing_address][street_address]=8094%20BEACH%20LANE&
renewals[][mailing_address][zip]=35023&
renewals[][driver_1][dl_number]=123&
renewals[][driver_1][last_name]=123&
renewals[][driver_1][state]=AL&
renewals[][driver_1][verified]=1&
renewals[][driver_2][verified]=0&
renewals[][id]=6412&
renewals[][insurance][expiration_date]=20130513&
renewals[][insurance][naic]=123&
renewals[][insurance][policy_number]=123&
renewals[][insurance][verified]=1&
renewals[][mailing_address][address_has_changed]=0&
renewals[][mailing_address][city]=HUEYTOWN&
renewals[][mailing_address][state]=AL&
renewals[][mailing_address][street_address]=123%20ANY%20LANE&
renewals[][mailing_address][zip]=35023&
renewals[][driver_1][dl_number]=123&
renewals[][driver_1][last_name]=123&
renewals[][driver_1][state]=AL&
renewals[][driver_1][verified]=1&
renewals[][driver_2][verified]=0&
renewals[][id]=6411&
renewals[][insurance][expiration_date]=20130513&
renewals[][insurance][naic]=123&
renewals[][insurance][policy_number]=123&
renewals[][insurance][verified]=1&
renewals[][mailing_address][address_has_changed]=0&
renewals[][mailing_address][city]=HUEYTOWN&
renewals[][mailing_address][state]=AL&
renewals[][mailing_address][street_address]=104%20MERRIMONT%20ROAD&
renewals[][mailing_address][zip]=35023&
JSON Parsed Params (params[:renewals]): https://gist.github.com/t2/5566652
Notice in the JSON that the driver_1 information is missing on the last record. Not sure why this is. The data is in the request. Any known bug I am missing? Let me know if you need more info.
Unfortunately this is just how Rails parses JSON like this (where your [] is massively nested). I've come up against this before - http://guides.rubyonrails.org/form_helpers.html#combining-them gave some explanation.
From what I remember, if you can put in numeric keys rather than just [] (i.e. [1] for the first one, [2] for the second etc.) then it will work as you want it to.
So I figured it out. I needed to set the requestSerializationMIMEType to RKMIMETypeJSON.

Can't turn array into a string error

I have a simple call
JSON.parse(Panda.get("/videos/#{self.panda_video_id}/encodings.json"))
Which returns :
can't convert Array into String
This is because the Panda.get("/videos/#(self.panda_video_id}/encodings.json") call returns an array in the new Panda 1.0.0 gem.
I also tried :
JSON.parse(Panda.get("/videos/#{self.panda_video_id}/encodings.json").to_s)
This returns:
705: unexpected token at 'created_at2010/07/19 20:28:13 +0000video_id4df3be7b6c6888ae86f7756c77c92d8bupdated_at2010/07/19 20:28:30 +0000started_encoding_at2010/07/19 20:28:21 +0000id6e2b35ad7d1ad9c9368b473b8acd0abcextname.mp4encoding_time0encoding_progress100file_size513300height110statussuccesswidth200profile_idf1eb0fe2406d3fa3530eb7324f410789'
Question
How would you turn the call at the top so that it returns a string?
does the following work:
panda_data = Panda.get("/videos/#{self.panda_video_id}/encodings.json")
JSON.parse(panda_data.to_s)
if it doesn't what is the error output?
If panda_data is an array, panda_data.to_s is guaranteed to return a string
Not that anyone had a chance at this, but
Panda_Gem since -v=0.6 has made all Panda.[get, post, etc.] requests return a hash. So you don't need the JSON.parse anymore. Removing the JSON.parse allows it to work.

Resources