const void*错误导致转换无效

Ell*_*n S 1 c++ iphone objective-c

我在.mm文件中运行以下代码,我收到错误:

Invalid conversion from 'const void*' to 'const __CFData*' 
Run Code Online (Sandbox Code Playgroud)

我需要在.mm中运行代码.如果我改为.m,它就不会抱怨.它为什么会这样?我编译到iPhone

  CFSocketNativeHandle native;
CFDataRef nativeProp = CFReadStreamCopyProperty(theReadStream, kCFStreamPropertySocketNativeHandle); 

 if(nativeProp == NULL)
{
    if (errPtr) *errPtr = [self getStreamError];
    return NO;
}

CFIndex nativePropLen = CFDataGetLength(nativeProp);
CFIndex nativeLen = (CFIndex)sizeof(native);

CFIndex len = MIN(nativePropLen, nativeLen);

CFDataGetBytes(nativeProp, CFRangeMake(0, len), (UInt8 *)&native);
CFRelease(nativeProp);

CFSocketRef theSocket = CFSocketCreateWithNative(kCFAllocatorDefault, native, 0, NULL, NULL);
if(theSocket == NULL)
{
    if (errPtr) *errPtr = [self getSocketError];
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

Geo*_*che 5

CFReadStreamCopyProperty()返回CFTypeRef,这只是一个typedeffor const void*,而C++对转换的要求比C(或Objective-C)更严格.你需要在这里明确地施放:

CFDataRef nativeProp = (CFDataRef)CFReadStreamCopyProperty(...);
Run Code Online (Sandbox Code Playgroud)