0

Я новичок в объективе-c, поэтому, пожалуйста, для терпения. Я просто скопировать и вставить код из учебника о запросе после в ИО и при реализации метода соединения я уже получил следующее сообщение об ошибке: enter image description here ViewController.hNSURLConnectionDelegate - Использование неопределенного идентификатора 'connection'

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <NSURLConnectionDelegate> 

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField; 
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField; 

- (IBAction)postRequest:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)postRequest:(id)sender { 

    NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@",@"randomUser",@"randomPassword"]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:/example.com/users/show.json"]]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 
    NSURLConnection * conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

    if(conn) { 
     NSLog(@"Connection Successful"); 
    } else { 
     NSLog(@"Connection could not be made"); 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
     _responseData = [[NSMutableData alloc] init]; 
    } 


    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     [_responseData appendData:data]; 
    } 

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse*)cachedResponse { 
     return nil; 
    } 

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
     NSString* respsoneString = [NSString stringWithUTF8String:[_responseData bytes]]; 
    } 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
     //Do something if there is an error in the connection 
    } 


} 
@end 
+2

Вы заявили методы внутри другого метода – Levi

+1

декларируют didReceiveResponse, didReceiveData, willCacheResponse, connectionDidFinishLoading, didFailWithError вне postRequest – iOSdev

+0

ок, спасибо за быстрый ответ, проблема решена – mkkrolik

ответ

-1

получил ответ NSURLConnectionDataDelegate метод включает в себя, что вызов фотографии в вашем коде

@interface ViewController : UIViewController <NSURLConnectionDelegate, NSURLConnectionDataDelegate> 

в своем коде вы не закрыли} тело метода над ним

Смежные вопросы