【问题标题】:NSURLConnectionDelegate - Use of undefined identifier 'connection'NSURLConnectionDelegate - 使用未定义的标识符“连接”
【发布时间】:2014-12-05 10:24:55
【问题描述】:

我是 Objective-c 的新手,所以请多多包涵。我刚刚从关于 ios 中发布请求的教程中复制并粘贴了代码,在实现连接方法期间,我遇到了以下错误: ViewController.h

#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

【问题讨论】:

  • 你在另一个方法中声明了方法
  • 在 postRequest 外声明 didReceiveResponse,didReceiveData,willCacheResponse,connectionDidFinishLoading,didFailWithError
  • 好的,感谢您的快速回复,问题已解决

标签: ios objective-c nsurlconnection nsurl nsurlconnectiondelegate


【解决方案1】:

收到的响应是NSURLConnectionDataDelegate 方法,在您的代码中包含该照片调用

@interface ViewController : UIViewController <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

在您的代码中,您没有关闭它上面的 } 方法体

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多