1

I want to work with the numbers from the array numbers in the function. I just want multiply 2 with 3, 3 with 3, 5 with 3 and 2 with 3. However, I get an error. What am I doing wrong?

import Foundation

let numbers = [2, 3, 5, 2];

func num(number: [Int]...) {
    for item in number {
        let result = item * 3
        print (result)
    }

}

num(numbers)

Binary operator '*' cannot be applied to operands of type '[Int]' and 'Int'

3
  • 1
    The problem is that number: [Int]... does not mean what you think it does. Commented Sep 6, 2015 at 23:34
  • remove in your func num(number: [Int]...) it ... Commented Sep 6, 2015 at 23:41
  • you said you wanted to multiply 2 with 3, 3 with 3, 5 with 3 and 2 with 3. check out my answer Commented Sep 6, 2015 at 23:57

4 Answers 4

1

You are passing an array of arrays to the function. The function parameter to num() should just be [Int] or Int...

EDIT

Difference between Int... and [Int]

with Int... you can pass a variable amount of params without explicitly using an array:

func num(mult: Int, arr: Int...){

    for n in arr{
        println("num = \(n * 3)")
    }

}

num(1,2,3,4) // first param (1) == mult(iplier), rest are ints captured by arr

while with an array ([Int]), you have to explicitly pass an array:

func num(mult: Int, arr: [Int]){

    for n in arr{
        println("num = \(n * 3)")
    }

}

num(1, [2,3,4]) // 1 == mult(iplier,), rest are the Ints to operate with 

Second solution is clearly cleaner.

I have never personally used Int..., but it definitely has its place... Needless to say that those Int... params need to go at the end of the parameter list... (see @Qbyte's comment below on why this statement is wrong)

Sign up to request clarification or add additional context in comments.

5 Comments

What is the difference between [Int] and Int...?
As of Xcode 7 beta 6 variadic parameters (i.e. Int...) can now appear anywhere in the parameter list not only at the end.
@Qbyte The statement about it coming at the end always was just an assumption because how would the compiler be able to distuingish between the params ? Sure, I can imagine how it would work with non-default parameters, but what if you had default parameters following the variadic parameter of the same type ? Or is that something you would have to take into account yourself, in which case my statement would still be kind of valid wrt correctness?
@Qbyte Sorry, I shouldn't answer things off the top of my head. I guess you'd have to specify the param "key" after the variadic param anyway, in which case your statement is correct...
@the_critic This should be a citation of those release notes: adcdownload.apple.com/Developer_Tools/Xcode_7_beta_6/…
1

Either delete the ... to be able to pass an array :

let numbers = [2, 3, 5, 2]

func num(number: [Int]) {
    for item in number {
        let result = item * 3
        print(result)
    }

}

num(numbers)

or you can pass the values directly with a vararg:

func num(number: Int...) {
    for item in number {
        let result = item * 3
        print(result)
    }

}

num(2, 3, 5, 2)

Comments

1

You are passing an array of arrays of Int (Array<Array<Int>>).

Array<Int>, which is [Int], which is Int..., but Int... parameter value can't be an array of [Int], it should be sequence of Ints. E.g.

func foo(bar: Int...) {
    // bar is [Int]
}

foo(0, 3, 13, 6) // parameter is sequence of `Int`s

Also, you could use .map() to apply transform to each element of array. And, using Int type as parameter is not efficient way of writing Swift code (you an not pass Array<UInt> as input parameter, etc.), protocol IntegerArithmeticType provides functions for * operator. All default Swift integer types conform to this protocol. So, the only way here is to use generic function with type T, where T: IntegerArithmeticType. Here is the final code:

let numbers = [2, 3, 5, 2]

/// Multilplies each element of array of `IntegerArithmeticTypes` by `multiplier`.
///
/// - Parameter multiplier: `IntegerArithmeticTypes` multiplier for each element of array.
/// - Parameter vals:        Array of `IntegerArithmeticTypes` in which each element 
///                          will be multiplied by `multiplier`
///
/// - Returns: Array with multiplied values from `values`
public func multipliedMap<T: IntegerArithmeticType>(multiplier: T, _ vals [T]) -> Array<T> {
    return vals.map  { $0 &* multiplier }
}

dump(multipliedMap(3, numbers)) 
// prints:
//
// ▿ 4 elements
// - [0]: 6
// - [1]: 9
// - [2]: 15
// - [3]: 6

PS: I used &* operator, because IntegerArithmeticType value may potentially overflow.

1 Comment

Since I'm a beginner to Swift and programming in general, I prefer the first answer of @Kametrixom because it seems to be way easier and less complicated. But thanks for your input. If I get better it may seem easier.
0

Try this :

 var numbers = [2, 3, 5, 2];

func num(number: [Int]) 
{
   var result = 0
    for var j  = 0; j < number.count - 1  ; j++
    {
        result = number[j] * number[j + 1]
        print ("\(result)")
     }
}

print(num(numbers))

5 Comments

It's a bit overkill, don't you think? But thanks for that input.
but you said you wanted to multiply each index by each other
Well, first of all it gives me more than 4 solutions and second, some of them are even wrong. But again, thanks for trying to help. :-) Your answer is interesting and I'll look into it more tomorrow. :-)
Naah, I should get 4. 2*3=6, 3*3=9, 3*5=15 and again 2*3=6.
Just change the code you should get 3 answers 2 *3 and 3*5 and 5*2 , that's how it should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.