我将%令牌声明为后置修复操作符,以便计算百分比但Xcode报告
`% is not a postfix unary operator`
Run Code Online (Sandbox Code Playgroud)
我的测试代码下面是基于发现的例子在这里.我还检查了Apple的最新文档,其中包含了操作员声明的语法,但它没有解释为什么Xcode会抱怨.
如何计算使用百分比%?假设我让它工作,那么我将如何恢复使用%同一类中其他地方的另一个函数的模运算?
有人可以根据我在Playground中的代码提供一个工作示例吗?
1.%含义百分比
postfix operator %
var percentage = 25%
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
Run Code Online (Sandbox Code Playgroud)
2.%表示余数
let number = 11
let divisor = 7
print(number % divisor)
Run Code Online (Sandbox Code Playgroud)
只是移动
var percentage = 25%
Run Code Online (Sandbox Code Playgroud)
下
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
Run Code Online (Sandbox Code Playgroud)
像这样
postfix operator %
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
var percentage = 25%
Run Code Online (Sandbox Code Playgroud)
原因:您的代码可以在应用程序中运行,但不能在游乐场中运行,因为游乐场从上到下解释代码.它没有看到postfix func下面的声明var percentage,因此var percentage它会给你一个错误,因为它还不知道如何处理它.
| 归档时间: |
|
| 查看次数: |
5149 次 |
| 最近记录: |