Pritesh Patel
Pritesh Patel

Reputation: 119

Use of undeclared identifier Objective-C Code

I am new to Objective-C coding ,please bear with me to ask if this is simple question

My Header file:

#import <Cocoa/Cocoa.h>

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

@end

My Implementation file:

#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 in implementation file it is showing undeclared identifier. if declare that variable in .h file its saying cannot declare variable inside @interface and protocol

Upvotes: 3

Views: 15674

Answers (2)

Boklucius
Boklucius

Reputation: 1926

You have to declare the variable before referencing it: NSMutableData* receivedData = [NSMutableData dataWithCapacity: 0];

Upvotes: 0

Antonio MG
Antonio MG

Reputation: 20410

As you can see in the comment:

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

receivedData should be declared somewhere. Try this:

NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];

Upvotes: 3

Related Questions