Note that there are some explanatory texts on larger screens.

plurals
  1. POUndefined key when iterating through NSDictionary
    text
    copied!<p>I am pretty new to iPhone/Objective-c programming. I'm struggling with an issue that doesn't make sense to me. I am writing an app that, as part of the code, will consume restful services. The method I am writing to make Post calls, using the ASIHttpRequest library, looks like this:</p> <pre><code>- (NSString *) httpPostStringResponseUrl:(NSURL *)url:(NSDictionary *)args{ ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; NSArray *keys = [args allKeys]; NSEnumerator *e = [args keyEnumerator]; NSString* object; while (object = [e nextObject]) { NSLog(@"Adding to args key: %@", object); [request setPostValue:[keys valueForKey:object] forKey:object]; } NSLog(@"Starting http POST request"); [request startSynchronous]; NSString *response = [request responseString]; return response; } </code></pre> <p>The idea is to pass in a dictionary of arguments and their values, and use them as the arguments to the POST request. However, when I run this code I get an NSUnknownKeyException. </p> <p>This doesn't make sense to me. The only access to the dictionary I am doing is using the keys provided by the iterator. So, how can they be part of the iterator and not be valid keys?</p> <p>For completeness, here is the code where I call the above function:</p> <pre><code>NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease]; NSURL *url = [[[NSURL alloc] initWithString:@"http://myURL"] autorelease]; [params setValue:@"user" forKey:@"Username"]; [params setValue:@"1234" forKey:@"Password"]; NSString *response = [web httpPostStringResponseUrl:url :params]; NSLog(@"Response is \"%@\"", response); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload