未声明的标识符Objective-C代码的使用

3

我刚开始学习Objective-C编程,请包容我,如果这是一个简单的问题,请帮助解答。

我的头文件:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;

@end

我的实现文件:

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{    
    // Insert code here to initialize your application

    // Listing 2-1  Creating a connection using NSURLConnection

    // 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.
    }
}

@end

在实现文件中的receivedData未声明。如果在.h文件中声明该变量,则会显示不能在@interface和协议内部声明变量


1
receivedData是在其他地方声明的实例变量。声明在哪里? - Larme
2个回答

3

正如您在评论中所看到的:

// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData dataWithCapacity: 0];

receivedData应该在某个地方声明。尝试这样做:

NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];

我应该在头文件中声明还是在实现文件中声明? - Pritesh Patel
根据您想要的可见性,在头文件中是公共的,在实现中将是私有的。 - Antonio MG

0
在引用变量之前,您必须先声明它: NSMutableData* receivedData = [NSMutableData dataWithCapacity: 0];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接