将 ASHIHTTPRequest 设置为 NSDictionary

发布于 2024-11-10 08:22:05 字数 677 浏览 3 评论 0 原文

我正在尝试将 NSDictionary 发送到服务器并将其设置如下:

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];

我收到以下错误:

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

这是令人费解的,因为媒体类型是字典中的第 14 个键,所以它怎么可能对所有其他的然后就因为这个而发脾气?

它似乎不接受我的词典,但有什么原因吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-11-17 08:22:05

有几个原因:

  • 字典中的键没有排序。不存在“字典中的第 14 个键”这样的东西。字典条目必须序列化才能记录,但不能保证其他方法会以相同的顺序访问它们。
  • ASIFormDataRequestASIHTTPRequest 都不会覆盖 -setValueForKeysWithDictionary:-setValue:forKey:。这意味着,除非键与对象的键值编码兼容键匹配,否则您将看到该异常。

尝试使用受支持的方法,例如 -setPostValue:forKey:

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

将来:使用 来源

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文