开发者

Setting ASHIHTTPRequest to NSDictionary

开发者 https://www.devze.com 2023-03-09 01:21 出处:网络
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];

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 setValue开发者_C百科sForKeysWithDictionary: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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消