Chris
Chris

Reputation: 12181

Setting ASHIHTTPRequest to NSDictionary

I'm trying to send an NSDictionary to a server and have it set up as follows:

NSDictionary *songInfo = [[NSDictionary alloc] initWithObjects:propertyValues forKeys:propertyKeys];

NSURL *exampleURL = [NSURL URLWithString:@"http://www.example.com/activities"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:exampleURL];

[request setValuesForKeysWithDictionary:songInfo];
[request startAsynchronous];

I'm getting the following error:

[<ASIFormDataRequest 0x1035600> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 1 Media Type.

Which is puzzling because Media Type is the 14th key in the Dictionary, so how could it have been fine with all the other ones and then flipped out on this?

It seems to not be accepting my Dictionary, but is there any reason why?

Upvotes: 1

Views: 1012

Answers (1)

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

There are several reasons:

  • The keys in a dictionary are not ordered. There is no such thing as a "14th key in [a] Dictionary." The dictionary entries have to be serialized for logging, but there is no guarantee they will be accessed in the same order by other methods.
  • Neither ASIFormDataRequest nor ASIHTTPRequest override -setValueForKeysWithDictionary: or -setValue:forKey:. This means that, unless the keys match up to key-value-coding–compliant keys of the object, you are going to see that exception.

Try using a supported method, like -setPostValue:forKey::

NSDictionary *songInfo = /*...*/;
ASIFormDataRequest *request = /*...*/;
[songInfo enumerateKeysAndObjectsUsingBlock:
^(id key, id value, BOOL *stop) {
    [request setPostValue:value forKey:key];
}];

And in future: USE THE SOURCE.

Upvotes: 2

Related Questions