0

I've found that when using NSDictionary to create the params for an AFNetworking POST request the behaviour is inconsistent. I have multiple POSTs where the params arrive at the server in the same order I created them in the NSDictionary and now creating a new request they are arriving at the server in a different order.

This is how I send them:

NSDictionary *params = @{@"username": @"testuser", @"count": @"6"};

But this is how it arrives on the server:

{"count":"6","username":"testuser"}

Its important for me that it arrive at the server exactly the same as it leaves my app as I hash together values for integrity and can't verify if it arrives in a different order to how it leaves.

I know NSDictionary is by nature not expected to keep the order, is there anyway using NSDictionary or another way I can guarantee the order?

Thanks in advance

adamtrousdale
  • 415
  • 6
  • 18
  • Seems ill-advised to build in that dependency. You're dependent not only on the order of the data items, but also the precise presentation, including the presence/absence of whitespace, etc. Easy to get burned down the road. – Hot Licks Feb 23 '15 at 17:42
  • I appreciate that, what I'm more interested in however is why it has worked for around 20 POSTs and suddenly it is passing in the incorrect order. I take measures to remove leading and trailing whitespace and had never had any issues so its of no concern to me. – adamtrousdale Feb 23 '15 at 17:47
  • Seems to me the simplest approach would be to copy the parms into an array in a given order, or sort them once in the array. – Hot Licks Feb 23 '15 at 17:48
  • have you explored the option of using NSArray? http://stackoverflow.com/questions/638129/how-to-declare-a-two-dimensional-array-of-string-type-in-objective-c – Sail Feb 23 '15 at 19:44
  • You'll need to use an array and your own request serializer – David Snabel-Caunt Feb 24 '15 at 11:26

1 Answers1

1

You'd have to use an ordered dictionary class. Unfortunately, there's not one in the Foundation framework, but it's fairly easy to create (or even download) your own. This article describes how to create an ordered dictionary class in Objective-C, and includes source code.

mipadi
  • 398,885
  • 90
  • 523
  • 479