0

我在头文件中定义了一个结构:

#import <UIKit/UIKit.h>    
typedef struct ORIGINAL_QUOTA_DATA_tag
{
    byte    exch;          
    byte    complex_stat;                   
    char    contract[8];    
    byte    status;         
    byte    type;        
}ORIGINAL_QUOTA_DATA;
@interface NetTestAppDelegate : NSObject <UIApplicationDelegate> {
    CFSocketRef _socket;
    CFDataRef address;
    ORIGINAL_QUOTA_DATA oQuota;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;

在我的 .m 文件中:

    static void TCPServerConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info){
memset(&oQuota, 0, sizeof(oQuota));
}

但它会磨损: 在此处输入图像描述

4

1 回答 1

1

oQuota从 C 函数引用(它也是静态的,但这不是问题)。您的oQuota变量仅在您的 Objective-C 方法实现中“在范围内” NetTestAppDelegate

void myCFunction()
{
    // oQuota cannot be referenced here.
}

@implementation NetTestAppDelegate
- (void)myObjectiveCMethod
{
   // oQuota is in scope, can reference it.
}
@end

但是您可以做的是将对象传递给您的回调:

static void TCPServerConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info){
    memset(
      &((NetTestAppDelegate *)info)->oQuota,
      0,
      sizeof(((NetTestAppDelegate *)info)->oQuota)
    );
    // Not sure whether the sizeof works, try it.
}

在您的CFSocketContext中,您需要设置:

myCFSocketContext.info = self;
于 2011-10-10T10:04:59.450 回答