【问题标题】:Use of undeclared identifier Objective-C Code使用未声明的标识符 Objective-C 代码
【发布时间】:2014-03-06 10:39:40
【问题描述】:

我是 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

【问题讨论】:

  • “receivedData 是在别处声明的实例变量”:在哪里声明?

标签: objective-c macos cocoa


【解决方案1】:

正如你在评论中看到的那样:

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

receivedData 应该在某处声明。试试这个:

NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];

【讨论】:

  • 我应该在头文件或实现文件中声明它
  • 根据你想要的可见性,在头文件中是公开的,在实现中是私有的。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多