Such situation. I have a GET request.

- (IBAction)sendGet:(id)sender { NSString *tmenu = _textFieldGet.text; NSString *urlString = [NSString stringWithFormat:@"http://example.ru/menu.php?tmenu=%@", tmenu]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; [theRequest addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [theRequest setHTTPMethod:@"GET"]; NSURLConnection *connectionGet = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if(connectionGet){ } } 

Thus, I have a parameter that is taken from a text string in which the user enters a number from 1 to 4.

I also have a PickerView with 4 values. Yes, it looks silly, but I definitely need a categoryCollection as an Array.

 categoryCollection = [[NSArray alloc] initWithObjects:@"Choose category", @"Group",@"Faculty", @"Teacher", @"Auditory",nil]; numberOfCategoryCollection = [[NSArray alloc]initWithObjects:@"0", @"1", @"2", @"3", @"4", nil]; NSDictionary *categoryDictionary=[[NSDictionary alloc] initWithObjects:categoryCollection forKeys:numberOfCategoryCollection]; 

After selecting any value, it is displayed in the textField .

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ [_textFieldGet setText:[categoryCollection objectAtIndex:row] ]; // Отображает категорию в текстФилд} 

In the request, I need to pass exactly the number, and the textField is passed to the textField , not the key. How can I make the option name displayed for a user, and the key passed to the request? For these purposes, I created categoryDictionary , I thought, you can somehow use it.

Sorry if I explained it too intricately, these are my first steps in development. Thanks in advance for your help.

    2 answers 2

    Hi, if I understood correctly, you just need to create an associative check, you can use if isEqualToString: and substitute a number from the results. The variant with the associative dictionary is also not bad, swap keys and values, or get the key by value (cumbersome and additional checks are needed), but there are more effective options in terms of performance (if it matters) .. what's the question then? :)

    • Yes, it seems to me that I need a similar check. But I didn’t understand a little why to change the keys and values. "or take out the key by value" - this is exactly what I wanted to achieve, but it wasn't all that much. Thank! - Maxim Kuznetsov
    • You need to get a number on the selected string, that is, execute the following code: NSString numberForRequest = [aDictionary objectForKey: userString]; that's all, you have a number depending on the line selected by the user, and now you have the opposite, the numbers are keys, and the lines are values. - FreeGor
    • I have a small problem, as I understand it, with formatting. When I try to enter data from a textField here, nothing happens. numberForRequest = [categoryDictionary objectForKey:@[@"%@",_textFieldGet.text]]; But when I ask in an explicit form, then everything works. I can not understand what is wrong. numberForRequest = [categoryDictionary objectForKey:@"Group"]; - Maxim Kuznetsov
    • Just try: numberForRequest = [categoryDictionary objectForKey: textFieldGet.text]; or first NSString * aKey = textFieldGet.text; then numberForRequest = [categoryDictionary objectForKey: aKey]; - FreeGor

    Try reading about AFNEtworking:

    You can put the plugin to check the API r2m-plugin-ios .

    I did this:

     NSURL *urlBase = [NSURL URLWithString:@"http://example.com"]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:urlBase]; AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; manager.requestSerializer = requestSerializer; manager.securityPolicy.allowInvalidCertificates = YES; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"]; NSDictionary *parameters = @{@"email": _textEmail.text, @"password": _textPassword.text} }; [manager GET:@"/api/categories" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 
    • Immediately I can not estimate and answer whether it is required. Now I read about it, I'll try to figure it out. Thank you very much! - Maxim Kuznetsov