2017-04-07 111 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

相关问题