成员运算符“%"必须至少有一个“ViewController"类型的参数 [英] Member operator '%' must have at least one argument of type 'ViewController’
问题描述
我正在尝试使用自定义函数创建一个简单的 Swift 3 模板,用于在 Xcode 应用程序中使用后缀一元运算符计算百分比.这似乎是一个重复的问题,因为我上一篇文章中的接受的答案已经展示了如何在 Playground 中执行此操作.但我后来发现自定义函数在 Xcode 项目中的工作方式不同.
I am trying to create a simple Swift 3 template with a custom function for calculating percentage using postfix unary operator in an Xcode app. This may seem like a duplicate question because the accepted answer in my previous post already shows how to do this in Playground. But I have since found that the custom function doesn't work the same way in an Xcode project.
在下面的模板中,我在文件范围内声明了'operator'
(或者至少我相信我做到了).但是当 postfix 函数被声明时,Xcode 会提示
In the template below, I declared the ’operator' at file scope
(or at least I believe I did). But when the postfix function is declared, Xcode advises that
Operator '%' declared in type 'ViewController' must be 'static'
并提供了一个修复它来插入 static
.使用 static
插入 Xcode 然后建议
and offers a fix-it to insert static
. With static
inserted Xcode then advises
Member operator '%' must have at least one argument of type 'ViewController’.
谁能解释为什么 %
函数在 Xcode 项目中需要是 static
以及最后一条错误消息在同一行的上下文中意味着什么(见下文)?谢谢
Can anyone explain why the %
function needs to be static
in the Xcode project and what the last error message means in the context of the same line (see below) ? Thanks
草稿模板
import UIKit
postfix operator %
class ViewController: UIViewController {
var percentage = Double()
override func viewDidLoad() {
super.viewDidLoad()
percentage = 25%
print(percentage)
}
static postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
}
编辑模板
这是基于已接受答案的工作模板.我不明白在文件范围内声明运算符是什么意思.
Here's the working template based on the accepted answer. I hadn't understood what is meant by declaring the operator at file scope.
import UIKit
postfix operator %
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
class ViewController: UIViewController {
var percentage = Double()
override func viewDidLoad() {
super.viewDidLoad()
percentage = 25%
print(percentage)
}
}
脚注
基于已接受的答案,现在可以从同一项目中的其他文件访问分组在单个文件中的自定义运算符函数.要查看更多信息,请访问 这里.
Building on the accepted answer, custom operator functions grouped in a single file may now be accessed from other files in the same project. To see more visit here.
推荐答案
我在文件范围内声明了'operator'
I declared the ’operator' at file scope
不,你没有.您在范围内定义了它UIViewController
定义:
No, you didn't. You defined it in the scope of the
UIViewController
definition:
postfix operator %
class ViewController: UIViewController {
// ...
static postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
}
可以将运算符定义为 Swift 3 中某个类型的静态成员函数,但前提是它们至少采用该类型的一个参数.
One can define operators as static member functions of a type in Swift 3, but only if they take at least one argument of that type.
将声明移动到文件范围以解决问题:
Move the declaration to the file scope to fix the problem:
postfix operator %
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
class ViewController: UIViewController {
// ...
}
这篇关于成员运算符“%"必须至少有一个“ViewController"类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!