我在.mm文件中运行以下代码,但出现错误:

Invalid conversion from 'const void*' to 'const __CFData*'

我需要在.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;
}

最佳答案

CFReadStreamCopyProperty()返回CFTypeRef,它只是typedefconst void*,并且C++在转换方面比C(或Objective-C)更加严格。您需要在此处显式转换:

CFDataRef nativeProp = (CFDataRef)CFReadStreamCopyProperty(...);

10-05 22:05