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

标签 objective-c macos cocoa

我是 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 和协议(protocol)中声明变量

最佳答案

正如您在评论中看到的:

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

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

NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];

关于objective-c - 使用未声明的标识符 Objective-C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221948/

相关文章:

iphone - 向兼容 iOS 3.0 的 UIView 添加轮廓阴影

ios - 获取请求以按最新消息的日期对对话进行排序

macos - Apple 系统日志到 ELK

当手位于魔术鼠标右侧时,Cocoa 会检测到

cocoa - 如何在 Cocoa 和 IB 中创建类似 Keynote 的界面?

objective-c - 有没有办法在访问特定属性时生成编译器警告?

php - 如何在 OS X 上连接到正确的 MySQL 数据库? (两个 mysqld 实例正在运行)

swift - IOCreatePlugInInterfaceForService 返回神秘错误

ios - iOS版本检查UIBarButtonSystemItemPageCurl是否存在

ios - 为什么 SKEmitterNode 会破坏累积的帧?