2014-03-06 31 views
2

我是新來的Objective-C代碼,請多多包涵,如果這是簡單的問題使用未聲明的標識符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 

at receivedData在實現文件中顯示未聲明的標識符。如果申報的.h變量報送說cannot declare variable inside @interface and protocol

+1

「receivedData是其他地方聲明實例變量」:申報在哪裏? – Larme

回答

3

正如你可以在註釋中看到:

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

receivedData應某處聲明。試試這個:

NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0]; 
+0

我應該在頭文件或實現文件中聲明它 –

+0

根據你想要的可見性,在頭文件中是公開的,在執行時將是私有的。 –

0

你必須引用它之前聲明的變量: NSMutableData* receivedData = [NSMutableData dataWithCapacity: 0];