成员运算符'%'必须至少有一个类型为'ViewController'的参数 [英] Member operator '%' must have at least one argument of type 'ViewController’

查看:112
本文介绍了成员运算符'%'必须至少有一个类型为'ViewController'的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的Swift 3模板,其中包含一个自定义函数,用于在Xcode应用程序中使用postfix一元运算符计算百分比。这似乎是一个重复的问题,因为我之前的帖子中的接受的答案已经显示了如何在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.

下面的模板中,我在文件范围中声明了'运算符'(或者至少我相信我做过)。但是当声明后缀函数时,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' 

并提供修复 - 插入静态。随着静态插入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)
    }
}

EDITED模板

这是基于接受答案的工作模板。我没有理解在文件范围内声明操作符的含义。

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)
    }
}

FOOTNOTE

在接受的答案的基础上,现在可以从同一项目中的其他文件访问分组在单个文件中的自定义操作员功能。要查看更多信息,请访问这里

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.

推荐答案


我在文件范围内声明'运营商'

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆