Get Xcode 5 to warn about new API calls

后端 未结 4 1190
北恋
北恋 2020-12-11 12:47

There was a method for Xcode 4.x:

#define __AVAILABILITY_TOO_NEW __attribute__((deprecated(\"TOO NEW!\"))) __attribute__((weak_import))

#if __IPHONE_OS_VERS         


        
4条回答
  •  攒了一身酷
    2020-12-11 13:23

    As of Xcode 5.0 it is not possible to get warnings from new API calls by redefining macros.

    Redefining a macro does show up in autocompletion and preprocessed preview (Assistant Editor > Preprocess) but does not trigger a warning despite using deprecate or unavailable keywords.

    Xcode 5.0.1+ does show a warning so both __AVAILABILITY_INTERNAL__IPHONE_X_X and NS_AVAILABLE_IOS can now be redefined as mentioned in the question and @borrrden answer.

    Code available as a Gist and as a library:

    #define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_5_0
    
    #ifndef __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED
        #define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_OS_VERSION_MIN_REQUIRED
    #endif
    
    #if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_OS_VERSION_MIN_REQUIRED
        #error You cannot ask for a soft max version which is less than the deployment target
    #endif
    
    #define __NBU_AVAILABILITY_STARTING(version) __attribute__((deprecated("Only available in iOS " version "+"))) __attribute__((weak_import))
    
    #if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_2_0
        #undef  __AVAILABILITY_INTERNAL__IPHONE_2_0
        #define __AVAILABILITY_INTERNAL__IPHONE_2_0 __NBU_AVAILABILITY_STARTING("2.0")
        #define __NBU_APICHECK_2_0(_ios)            __NBU_AVAILABILITY_STARTING("2.0")
    #else
        #define __NBU_APICHECK_2_0(_ios)            CF_AVAILABLE_IOS(_ios)
    #endif
    
    #if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_2_1
        #undef  __AVAILABILITY_INTERNAL__IPHONE_2_1
        #define __AVAILABILITY_INTERNAL__IPHONE_2_1 __NBU_AVAILABILITY_STARTING("2.1")
        #define __NBU_APICHECK_2_1(_ios)            __NBU_AVAILABILITY_STARTING("2.1")
    #else
        #define __NBU_APICHECK_2_1(_ios)            CF_AVAILABLE_IOS(_ios)
    #endif
    
    //...
    
    #if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_7_0
        #undef  __AVAILABILITY_INTERNAL__IPHONE_7_0
        #define __AVAILABILITY_INTERNAL__IPHONE_7_0 __NBU_AVAILABILITY_STARTING("7.0")
        #define __NBU_APICHECK_7_0(_ios)            __NBU_AVAILABILITY_STARTING("7.0")
    #else
        #define __NBU_APICHECK_7_0(_ios)            CF_AVAILABLE_IOS(_ios)
    #endif
    
    #undef  NS_AVAILABLE_IOS
    #define NS_AVAILABLE_IOS(_ios)                  __NBU_APICHECK_##_ios( _ios )
    
    #undef  __OSX_AVAILABLE_BUT_DEPRECATED
    #define __OSX_AVAILABLE_BUT_DEPRECATED(_osx, _osxDep, _ios, _iosDep)            __AVAILABILITY_INTERNAL##_ios
    
    #undef  __OSX_AVAILABLE_BUT_DEPRECATED_MSG
    #define __OSX_AVAILABLE_BUT_DEPRECATED_MSG(_osx, _osxDep, _ios, _iosDep, _msg)  __AVAILABILITY_INTERNAL##_ios
    

提交回复
热议问题