Member Operator '%' Must Have at Least One Argument of Type 'Viewcontroller'

Member operator '==' must have at least one argument of type

You need to make your Rectangle protocol a class. Try like this:

protocol Rectangle: class, Equatable {
var width: Double { get }
var height: Double { get }
}

class Equality: Rectangle {
internal var width: Double = 0
internal var height: Double = 0
static func ==(lhs: Equality, rhs: Equality) -> Bool {
return lhs.width == rhs.width && rhs.height == lhs.height
}
}

or simply:

protocol Rectangle: Equatable {
var width: Double { get }
var height: Double { get }
}

extension Rectangle {
static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs.width == rhs.width && rhs.height == lhs.height
}
}

class Equality: Rectangle {
internal var width: Double = 0
internal var height: Double = 0
}

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

I declared the ’operator' at file scope

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

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 {

// ...

}

How to make a custom function with + operator in Swift

As explained by @MartinR in the comments, you need to put this function at the top level (NOT inside a class).

import Foundation

// Will compile fine from here as is
func + <T>(el: T, arr: [T]) -> [T] {
var ret = arr
ret.insert(el, at: 0)
return ret
}

class TableGenerator {
// will require at least one argument of the function to be of `TableGenerator` type
}

How to access a custom function from any file in the same Swift project?

Any function declared at file scope will have the implicit scope internal and will be visible in the rest of the project/module.

I recommend reading the access control guide for more information.

Edit:

I'm still not sure what you're trying to do, but it looks like you are mixing global functions with static methods and custom operators.

If what you want is to declare a custom operator that you can use in any other file in the project, the solution is in the documentation that you linked in your question.

So this is what you need to declare a custom % (as you defined it) for the Int type:

CustomOperators.swift

postfix operator %

extension Int {

static postfix func % (n: Int) -> Double {
return Double(n) / 100
}

}

main.swift

print(90%) //outputs "0.9"

That's all there is to it. You simply declare the operator globally: postfix operator % and you define the operator function as a static method in a extension on the Int type.

Now you can use your new operator in other files (like I did in main.swift).

How to calculate percentage using % as a postfix unary operator in Swift 3 and still be able to use % for modulo?

Just move

var percentage = 25%

under

postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}

Like so

postfix operator %

postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}

var percentage = 25%

Reason: your code would work in an app, but not in a playground, because a playground interprets the code from top to bottom. It doesn't see the postfix func declaration located below var percentage, so at the point of var percentage it gives you an error, because it doesn't yet know what to do with it.

Instance member cannot be used on type

You just have syntax error when saying = {return self.someValue}. The = isn't needed.

Use :

var numPages: Int {
get{
return categoriesPerPage.count
}

}

if you want get only you can write

var numPages: Int {
return categoriesPerPage.count
}

with the first way you can also add observers as set willSet & didSet

var numPages: Int {
get{
return categoriesPerPage.count
}
set(v){
self.categoriesPerPage = v
}
}

allowing to use = operator as a setter

myObject.numPages = 5

How to compare UIViewController in Swift 3?

If you want to compare to a particular view controller you have to compare their refererences.

Try this...

if(vc === viewController) )
{
return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
}

Why doesn't this Javascript RGB to HSL code work?

The resulting HSV array has to be interpreted as three fractions. For some programs, if you want to express HSV as integers, you multiply the "H" value by 360 and the "S" and "V" values by 100. The HSV value you quote for your green shade RGB[126, 210, 22] is HSV [87, 81, 45] in integers. You could change the function to return such integers if you want to:

function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;

if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}

return [Math.floor(h * 360), Math.floor(s * 100), Math.floor(l * 100)];
}

[edit] that said, it's still giving me something with a brightness ("L" or "V") that's considerably too dark; Gimp says that the HSV value should be [90, 80, 82], or in fractional terms [.20, .80, .82].

[another edit] well one problem could be that HSL and HSV are different schemes ... still looking around.

OK in case anybody wants RGB to HSV (like you'd see in Gimp for example) here's a version of that:

function rgbToHsv(r, g, b) {
var
min = Math.min(r, g, b),
max = Math.max(r, g, b),
delta = max - min,
h, s, v = max;

v = Math.floor(max / 255 * 100);
if ( max != 0 )
s = Math.floor(delta / max * 100);
else {
// black
return [0, 0, 0];
}

if( r == max )
h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
h = 2 + ( b - r ) / delta; // between cyan & yellow
else
h = 4 + ( r - g ) / delta; // between magenta & cyan

h = Math.floor(h * 60); // degrees
if( h < 0 ) h += 360;

return [h, s, v];
}

edit note that a couple comments suggest that Math.round() might give better answers than Math.floor(), if anybody wants to experiment.



Related Topics



Leave a reply



Submit