2017-04-07 24 views
1

我正在創建一個具有類似任務的函數,但是隻能使用該平臺上的API。是否有可能在構建特定平臺時無法使用該功能?如何使一個功能可以訪問特定的平臺?

例子:

class myClass { 

    // How can I hide this function when I'm building for macOS? 
    class func myFunctionForIOS() { 

    } 
    // And hide this when building for iOS? 
    class func myFunctionForMACOS() { 

    } 
} 

回答

2

雨燕Preprocessor Directives可以做到這一點:

func myFunction() { 
    #if os(macOS) 
    // do something 
    #elseif os(iOS) 
    // do something else 
    #else 
    // do a final thing 
    #endif 
} 

有點更多信息here too

1
class ClassA{ 
    //define common method 
    virtual void MethodA() = 0; 
} 

class ClassA_iOS_Impl: public ClassA{ 
    //override your implement for iOS 
    void MethodA() override {//call iOS specific API} 
} 

class ClassA_Mac_Impl: public ClassA{ 
    //override your implement for Mac 
    void MethodA() override {//call Mac specific API} 
} 

然後根據您的平臺構建不同的文件。

+0

這也是一個很好的解決方法,它交換模塊化定製的本地化。對於預處理器指令和虛擬方法都有爭論。 – ColGraff

相關問題