objective-c - 使用未声明的标识符

标签 objective-c macos cocoa

我是 Objective-C 编码新手,请耐心询问这是不是一个简单的问题 我的头文件:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*) response;

@end

我的实现文件:

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSMutableData *receivedData  =[NSMutableData dataWithCapacity: 0];

    // Create the request
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];

    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.                  
    receivedData = [NSMutableData dataWithCapacity: 0];

    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!theConnection) {
        // Release the receivedData object.
        receivedData = nil;
        // Inform the user that the connection failed.
     }
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSMutableData *receivedData =[NSMutableData dataWithCapacity:0];


    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse object.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}
@end

当我在实现 appDelegate 中运行代码时,它会发出警告

Method definition for connection:didRecieveResponse :not found

在哪里

-(void)connection line it gives "use of undelcared identifier connection did u mean Collection".

最佳答案

您不需要声明该方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*) response; 

如果您在代码中使用 NSURLConnectionDataDelegate 协议(protocol),则在头文件中。只需在实现中添加协议(protocol)并使用该协议(protocol)中的方法即可。

关于objective-c - 使用未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317536/

相关文章:

objective-c - XCODE 4.2 不显示崩溃行

ios - 在 performBlock 中多次保存上下文

iphone - ios:如何模仿Android viewflipper control/multiview tree of decision?

java - 如何将最新的 AppleJavaExtensions 与 Maven 一起使用?

macos - cocoa:从 Mail.app 读取邮件

objective-c - Cocoa Visual Format Language 中的 View 名称是什么?

macos - 无法使用 AVAudioPlayer 播放专辑

objective-c - 如何使用用户最喜欢的文件夹填充 NSPopUpButton

iPhone ABPeoplePickerNavigationController - 如何从地址簿中选择一个人的两个不同多值属性的两个单个条目

cocoa - 如何设置HUD窗口的透明度