在iOS开发过程中,集成第三方库的时候,难免会遇到库冲突,造成编译通不过。
一般的做法是,修改冲突库的类名。但是修改类名,可能就会污染第三方的代码,不好升级和维护。
现在我介绍一种不直接修改类名的情况解决库冲突的问题。
C语言中的宏预处理可以很好的解决类名冲突问题。
以AFNetworking为例,假设我们工程代码里面有AFNetworking,第三方库也集成了AFNetworking,编译的时候,就会报错:duplicate symbol _OBJC_IVAR_$_AFHTTPSessionManager._requestSerializer
我们在工程的预处理头文件中Prefix.h加一个宏定义
#define AFHTTPSessionManager KKAFHTTPSessionManager
在编译过程中,编译器在预处理阶段,把AFHTTPSessionManager替换成KKAFHTTPSessionManager,编译出来的库里面就没有AFHTTPSessionManager符号,只有KKAFHTTPSessionManager,也就没有duplicate symbol了。
代码详单如下:
#define AFHTTPSessionManager KKAFHTTPSessionManager
#define AFNetworkReachabilityManager KKAFNetworkReachabilityManager
#define AFSecurityPolicy KKAFSecurityPolicy
#define AFURLRequestSerialization KKAFURLRequestSerialization
#define AFURLResponseSerialization KKAFURLResponseSerialization
#define AFURLSessionManager KKAFURLSessionManager
#define AFHUDNotificationObserver KKAFHUDNotificationObserver
#define AFHTTPRequestSerializer KKAFHTTPRequestSerializer
#define AFJSONRequestSerializer KKAFJSONRequestSerializer
#define AFPropertyListRequestSerializer KKAFPropertyListRequestSerializer
#define AFQueryStringPair KKAFQueryStringPair
#define AFStreamingMultipartFormData KKAFStreamingMultipartFormData
#define AFMultipartBodyStream KKAFMultipartBodyStream
#define AFHTTPBodyPart KKAFHTTPBodyPart
#define AFHTTPResponseSerializer KKAFHTTPResponseSerializer
#define AFJSONResponseSerializer KKAFJSONResponseSerializer
#define AFXMLParserResponseSerializer KKAFXMLParserResponseSerializer
#define AFXMLDocumentResponseSerializer KKAFXMLDocumentResponseSerializer
#define AFPropertyListResponseSerializer KKAFPropertyListResponseSerializer
#define AFImageResponseSerializer KKAFImageResponseSerializer
#define AFCompoundResponseSerializer KKAFCompoundResponseSerializer
#define AFURLSessionManagerTaskDelegate KKAFURLSessionManagerTaskDelegate
#define AFStringFromNetworkReachabilityStatus KKAFStringFromNetworkReachabilityStatus
#define AFNetworkingReachabilityDidChangeNotification KKAFNetworkingReachabilityDidChangeNotification
#define AFNetworkingReachabilityNotificationStatusItem KKAFNetworkingReachabilityNotificationStatusItem
#define AFQueryStringPairsFromDictionary KKAFQueryStringPairsFromDictionary
#define ErrorDomain KKErrorDomain
#define AFQueryStringPairsFromKeyAndValue KKAFQueryStringPairsFromKeyAndValue
#define AFNetworkingTaskDidCompleteErrorKey KKAFNetworkingTaskDidCompleteErrorKey
#define AFNetworkingTaskDidCompleteResponseSerializerKey KKAFNetworkingTaskDidCompleteResponseSerializerKey
#define AFNetworkingTaskDidCompleteAssetPathKey KKAFNetworkingTaskDidCompleteAssetPathKey
#define AFNetworkingTaskDidCompleteSerializedResponseKey KKAFNetworkingTaskDidCompleteSerializedResponseKey
#define AFNetworkingTaskDidCompleteResponseDataKey KKAFNetworkingTaskDidCompleteResponseDataKey
#define AFNetworkingTaskDidCompleteNotification KKAFNetworkingTaskDidCompleteNotification
#define AFURLSessionDidInvalidateNotification KKAFURLSessionDidInvalidateNotification
#define AFNetworkingTaskDidResumeNotification KKAFNetworkingTaskDidResumeNotification
#define AFURLSessionDownloadTaskDidFailToMoveFileNotification KKAFURLSessionDownloadTaskDidFailToMoveFileNotification
#define AFNetworkingTaskDidSuspendNotification KKAFNetworkingTaskDidSuspendNotification
#define _AFURLSessionTaskSwizzling _KKAFURLSessionTaskSwizzling
#define AFNetworkingOperationFailingURLResponseErrorKey KKAFNetworkingOperationFailingURLResponseErrorKey
#define AFNetworkingOperationFailingURLResponseDataErrorKey KKAFNetworkingOperationFailingURLResponseDataErrorKey
#define AFStringFromNetworkReachabilityStatus KKAFStringFromNetworkReachabilityStatus
#define AFNetworkingReachabilityDidChangeNotification KKAFNetworkingReachabilityDidChangeNotification
#define AFNetworkingReachabilityNotificationStatusItem KKAFNetworkingReachabilityNotificationStatusItem
#define AFQueryStringPairsFromDictionary KKAFQueryStringPairsFromDictionary
#define ErrorDomain KKErrorDomain
#define AFQueryStringPairsFromKeyAndValue KKAFQueryStringPairsFromKeyAndValue
#define AFNetworkingTaskDidCompleteErrorKey KKAFNetworkingTaskDidCompleteErrorKey
#define AFNetworkingTaskDidCompleteResponseSerializerKey KKAFNetworkingTaskDidCompleteResponseSerializerKey
#define AFNetworkingTaskDidCompleteAssetPathKey KKAFNetworkingTaskDidCompleteAssetPathKey
#define AFNetworkingTaskDidCompleteSerializedResponseKey KKAFNetworkingTaskDidCompleteSerializedResponseKey
#define AFNetworkingTaskDidCompleteResponseDataKey KKAFNetworkingTaskDidCompleteResponseDataKey
#define AFNetworkingTaskDidCompleteNotification KKAFNetworkingTaskDidCompleteNotification
#define AFURLSessionDidInvalidateNotification KKAFURLSessionDidInvalidateNotification
#define AFNetworkingTaskDidResumeNotification KKAFNetworkingTaskDidResumeNotification
#define AFURLSessionDownloadTaskDidFailToMoveFileNotification KKAFURLSessionDownloadTaskDidFailToMoveFileNotification
#define AFNetworkingTaskDidSuspendNotification KKAFNetworkingTaskDidSuspendNotification
#define AFURLSessionTaskSwizzling KKAFURLSessionTaskSwizzling
#define AFNetworkingOperationFailingURLResponseErrorKey KKAFNetworkingOperationFailingURLResponseErrorKey
#define AFNetworkingOperationFailingURLResponseDataErrorKey KKAFNetworkingOperationFailingURLResponseDataErrorKey
#define AFURLRequestSerializationErrorDomain KKAFURLRequestSerializationErrorDomain
#define AFURLResponseSerializationErrorDomain KKAFURLResponseSerializationErrorDomain
#define _AFURLSessionTaskSwizzling _KKAFURLSessionTaskSwizzling