我是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文件中声明该变量,则说明cannot declare variable inside @interface and protocol
答案 0 :(得分:3)
正如您在评论中看到的那样:
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData dataWithCapacity: 0];
应该在某处声明receivedData。试试这个:
NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];
答案 1 :(得分:0)
您必须在引用变量之前声明变量:
NSMutableData* receivedData = [NSMutableData dataWithCapacity: 0];