如何在同名方法/属性 变量中调用全局函数?

How can I call a global function inside a method / property variable with the same name?

例如,我的 class 中有一个名为 ceil 的 属性。在函数内部,我想调用全局函数ceil。我该怎么做?

class Example : BaseClass {
    var ceil : Double { return ceil(self); } // Error: Cannot call value of non-function type 'Double';
    func round : Double { return round(self); } // Infinite recursive
    var flooring : Double { return floor(self); } // Success
}

但是如果我不想使用 ceiling。我想使用 ceil。在这种情况下我该怎么做?可能吗?我在想是否可以调用全局函数,例如 Math.ceil(self) 如果可能的话。

您可以使用var ceil : Double { return Darwin.ceil(self) }

您应该为您的用例使用扩展,无需创建新的 class。这样您就可以在需要时随时在 Double 上使用这些方法,而无需实例化新的 class

extension Double {
  var ceil : Double { return Darwin.ceil(self) }
  var floor : Double { return Darwin.floor(self) } // Success
}

您可以使用var ceil : Double { return Demo.ceil(self); }

这里Demo是项目名称。