FMDatabaseQueue dispatch_queue_set_specific -Wunguarded-availabilit

18 min read
/Users/pan/memos_notes/macos/Pods/FMDB/src/fmdb/FMDatabaseQueue.m:101:9: warning: 'dispatch_queue_set_specific' is only available on macOS 10.7 or newer [-Wunguarded-availability]
        dispatch_queue_set_specific(_queue, kDispatchQueueSpecificKey, (__bridge void *)self, NULL);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In module 'Foundation' imported from /Users/pan/memos_notes/macos/Pods/FMDB/src/fmdb/FMDatabaseQueue.h:9:
In module 'CoreFoundation' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:6:
In module 'Dispatch' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFStream.h:20:
dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
^
        dispatch_queue_set_specific(_queue, kDispatchQueueSpecificKey, (__bridge void *)self, NULL);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/pan/memos_notes/macos/Pods/FMDB/src/fmdb/FMDatabaseQueue.m:184:54: warning: 'dispatch_get_specific' is only available on macOS 10.7 or newer [-Wunguarded-availability]
    FMDatabaseQueue *currentSyncQueue = (__bridge id)dispatch_get_specific(kDispatchQueueSpecificKey);
                                                     ^~~~~~~~~~~~~~~~~~~~~
In module 'Foundation' imported from /Users/pan/memos_notes/macos/Pods/FMDB/src/fmdb/FMDatabaseQueue.h:9:
In module 'CoreFoundation' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:6:
In module 'Dispatch' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFStream.h:20:
dispatch_get_specific(const void *key);
^
    FMDatabaseQueue *currentSyncQueue = (__bridge id)dispatch_get_specific(kDispatchQueueSpecificKey);
                                                     ^~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
error: the following command failed with exit code 0 but produced no further output
CompileC /Users/pan/memos_notes/build/macos/Build/Intermediates.noindex/Pods.build/Debug/FMDB.build/Objects-normal/x86_64/FMDatabase.o /Users/pan/memos_notes/macos/Pods/FMDB/src/fmdb/FMDatabase.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler (in target 'FMDB' from project 'Pods')
/Users/pan/memos_notes/macos/Pods/Pods.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.6, but the range of supported deployment target versions is 10.9 to 12.3.99. (in target 'FMDB' from project 'Pods')

这是一个警告信息,提示在当前的编译环境下使用的 dispatch_queue_set_specific 方法只在 macOS 10.7 或更新的版本中可用。

这个警告通常是由于在编译时使用了较新的 API,而在目标平台上的操作系统版本过旧所导致的。由于该警告是一个 "unguarded-availability"(未保护的可用性)警告,这意味着代码中没有包含条件编译语句来检查当前的运行环境是否支持该 API,这可能导致应用在旧版本的操作系统上运行时出现崩溃或未定义的行为。

为了解决这个问题,你可以使用条件编译语句来检查当前运行环境的版本,从而避免在不支持该 API 的系统上使用它。在这种情况下,你可以在使用 dispatch_queue_set_specific 的代码前添加以下条件编译语句:

c
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
    // 使用 dispatch_queue_set_specific 方法
#else
    // 使用备用方案
#endif

这里 __MAC_OS_X_VERSION_MIN_REQUIRED 宏定义表示当前编译环境中指定的最低支持的 macOS 版本。在条件编译语句中,我们检查该宏定义的值是否大于或等于 1070(也就是 macOS 10.7),如果是,则说明当前运行环境支持使用 dispatch_queue_set_specific 方法,可以在这里使用它;否则,我们可以使用备用方案。