The% operator must have at least one argument of type ViewController

I am trying to create a simple Swift 3 template with a user-defined function for calculating a percentage using a postfix unary operator in an Xcode application. This may seem like a duplicate question, because the accepted answer in my previous post already shows how to do this on the Playground. But since then, I have discovered that a user-defined function does not work the same in an Xcode project.

In the template below, I declared 'operator' at file scope (or at least I think I did). But when the postfix function is declared, Xcode reports that

  Operator '%' declared in type 'ViewController' must be 'static' 

and offers a fix for entering static . Using static inserted Xcode then advise

  Member operator '%' must have at least one argument of type 'ViewController'. 

Can someone explain why the % function should be static in the Xcode project and what the last error message means in the context of the same line (see below)? Thanks

Project template

 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 Template

Here the work pattern is based on the accepted answer. I did not understand what is meant by the declaration of the operator in the file area.

 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

Based on the accepted answer, user-defined operator functions grouped into one file can now be accessed from other files in the same project. To learn more, visit here .

+4
source share
2 answers

I declared a 'operator in the file area

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

You can define operators as static member functions of a type in Swift 3, but only if they accept at least one argument of that type.

Move the ad to the file area to fix the problem:

 postfix operator % postfix func % (percentage: Int) -> Double { return (Double(percentage) / 100) } class ViewController: UIViewController { // ... } 
+9
source

another option if you want to use closures in Swift 3 :

 import UIKit typealias Filter = (CIImage) -> CIImage infix operator >>> func >>> (filter1: @escaping Filter, filter2: @escaping Filter) -> Filter{ return { image in filter2( filter1( image)) } } class ViewController: UIViewController { //... } 

Eidhof, Chris; Coogler, Florian; Swierstra, Wouter. Functional Swift: Updated for Swift 3 (Kindle Location 542). GbR Florian Kugler and Chris Eidhof. Kindle Edition.

+1
source

Source: https://habr.com/ru/post/1260692/


All Articles