如何使某个功能可以访问特定平台?

时间:2017-04-07 00:45:09

标签: ios swift xcode macos

我创建的类具有执行类似任务的功能,但其中一个使用的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 个答案:

答案 0 :(得分:2)

Swift有Preprocessor Directives可以做到这一点:

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

更多信息here too

答案 1 :(得分: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}
}

然后根据您的平台构建不同的文件。