Swift Cannot Invoke '*' with an Argument List of Type '(Int, Int)'

Cannot invoke initializer for type 'String' with an argument list of type '(Int?)'

Update

cell.orderprice.text = String(model.price)

with

if let priceOfProduct = model.price {
cell.orderprice.text = String(priceOfProduct )
}
else{
cell.orderprice.text = "";
}

Swift cannot invoke '*' with an argument list of type '(Int, Int)'

The compiler error is misleading.

The real issue is that you missed the declaration of the function return type, so the compiler infers Void and it gets confused when tries (and fails) to find a suitable overloading for * that returns Void.

Change your function to

func timesByHundred(d: Int) -> Int {
return d * 100
}

Cannot invoke initializer for type 'Int' with an argument list of type '(Number)'

This is running on the iPad application Swift Playgrounds.

They apparently have created a protocol Number behind the scenes to ease the pain of Swift type conversions. In this mini program, it is possible to multiply an Int and a Double without first converting the Int to a Double:

let a = 5      // a is an Int
let b = 6.3 // b is a Double
let c = a * b // This results in c being type Number

Number has read-only properties int and double which return the Int and Double representations of the number.

var int: Int { get }
var double: Double { get }

So, if you need index to be an Int, do it like this:

let index = note.int

Using numpy with swift throws cannot invoke '*' with an argument error

Could it be that you are not calling zeros correctly?

If what you are after is a 2x2 matrix you might want to take a look at the docs for Numpy as it seems you are falsely calling said method

Edit: My suggestion wasn't correct, but still it seems that the error lies in the syntax, take a look at this

Edit2: As per your comment, here some explanation. (TLDR at the bottom)

Edit3: My conclusion below seems to be wrong for the solution see answer by @markroxor below. However the difference between () and [] holds, thus I'll leave the paragraph as is.

I am actually not too familiar with swift so I can't assure you that everything i say is correct, but here goes.
I just assumed the arguments are passed to python in the way they are put into the method call. Now in Python x = [a,b,c] will create an object of type list whereas x = (a,b,c) will create a tuple. They both are used to store multiple elements which can be accessed via x[index]. The most important difference between them being, that a list can be modified after its creation (x[0] = z) while attempting this on a tuple will throw an error. However this does not apply to Swift (contrary to what i thought at first)

Now, seeing how python greatly ignores parameter types as long as nothing "illegal" is attempted with them, I thought it was probable that numpy.zeros() would not care if it is passed a list or a tuple as long as it contains integers (tested it, no errors). Again this applies for use in python itself only

Now after looking up Swift syntax I would say that the problem/solution is that while Swift and Python seem to handle lists/arrays (Python calls them lists) very similarly the syntax of tuples differs greatly, causing the method call to throw an error when using round braces, thereby creating a Swift Tuple which python doesn't know how to handle, whereas the similarity in how arrays/lists work leads to [] being successfully passed to the numpy method

TLDR
The difference seems to be that while Python and Swift handle arrays similarly, the implementation of Tuples differs, so that would be the reason using [] works over ()

Error Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)' with nil-coalescing operator

You can simply do,

let int64Value: Int64 = 123
let str = String(int64Value)

Edit:

Since detailItem?.count is optional, you need to unwrap it before passing it to the String initializer. You can use nil coalescing operator to unwrap and provide a default value, i.e.

cell.textLabel?.text = String(detailItem?.count ?? 1)

Cannot invoke 'CGRect.Type.init' with an argument list of type '(x: Int, y: Int, width: Float, height: Float)'

CGRect.init has three different versions, for the three types of arguments it accepts - Int, Double, and CGFloat. Whatever values you're passing into x, y, width, and height must be the same type. To fix this you might try casting your values to Double by wrapping them in Double().


Regarding your comment, there's no version of CGRect.init() that takes Float parameters. Cast your Floats to Double and it should work.

Swift 4 Cannot invoke 'index' with an argument list of type

This is almost certainly just an extension of the fact that protocols (aka. existentials) don't conform to themselves. So the class existential UIViewController & MyDelegate doesn't conform to Equatable, even though UIViewController does.

Therefore because index(of:) is constrained to being called on a Collection with Equatable elements, you cannot call it on a [UIViewController & MyDelegate].

Here's a more minimal example:

protocol P {}
protocol X {}
class Foo : P {}

func foo<T : P>(_ t: T) {}

func bar(_ f: Foo & X) {
// error: Protocol type 'Foo & X' cannot conform to 'P' because only concrete
// types can conform to protocols
foo(f)
}

We cannot pass f as an argument to foo(_:) as Foo & X doesn't conform to P, even though Foo does. However really this should be a clear-cut case of where the existential should always be able to conform to itself, so I went ahead and filed a bug.

Until fixed, a simple solution is just to do an intermediate cast to the concrete type – so in our example, we can do:

foo(f as Foo)

and in your example, you can do:

let index = (self.viewControllers as [UIViewController]).index(of: myController) 

Swift 3 compiler displays wrong message when returning a value from a void function

CGPoint has three overloaded initializers with the same argument names:

 public init(x: CGFloat, y: CGFloat)
public init(x: Double, y: Double)
public init(x: Int, y: Int)

You'll get the same error message with

let v: Void = CGPoint(x: 1.0, y: 2)
// error: cannot invoke initializer for type 'CGPoint' with an argument list of type '(x: Double, y: Int)'
// note: overloads for 'CGPoint' exist with these partially matching parameter lists: (x: Int, y: Int), (x: Double, y: Double)

Here is another example demonstrating the effect:

struct A {
init(x: Double, y: Double) { }
}

struct B {
init(x: Double, y: Double) { }
init(xi: Int, yi: Int) { }
}

struct C {
init(x: Double, y: Double) { }
init(x: Int, y: Int) { }
}

let v1: Void = A(x: 1, y: 2.0)
// error: cannot convert value of type 'A' to specified type 'Void' (aka '()')

let v2: Void = B(x: 1, y: 2.0)
// error: cannot convert value of type 'B' to specified type 'Void' (aka '()')

let v3: Void = C(x: 1.0, y: 2.0)
// error: cannot convert value of type 'C' to specified type 'Void' (aka '()')

let v4: Void = C(x: 1, y: 2.0)
// error: cannot invoke initializer for type 'C' with an argument list of type '(x: Int, y: Double)'

Only in the last line, C(x: 1, y: 2.0) matches two initializers.
I assume that the compiler tries to match against both, fails to convert
the return value to Void in both cases, and therefore emits a different
error message.

The error message is not very helpful, so you might consider to file
a bug report at http://bugs.swift.org.

Cannot invoke 'value' with an argument list of type '(String)'

You probably need to change them to (message as AnyObject).value(forKey:"···") (adding the forKey: label).

Do you know what kind of object message is supposed to be? Repeatedly casting it to AnyObject is odd. It would be cleaner to create a new variable - let messageObject = message as AnyObject and then call messageObject.value(forKey:"···"). (I suspect you really want to cast it to Dictionary or something like that, in which case you can do messageDictionary["···"] instead of calling value(forKey:).)

Also, in Swift you can do this to reduce redundancy even more:

if let content = messageObject.value(forKey:"content") as? String {
stringContent = content
}


Related Topics



Leave a reply



Submit