0

I have a question about Swift. Here is my code:

var theArray = [1, 2, 3]

aFunction(value: 2)

func aFunction(value: Int) -> (Int, Int, Int){
    var value1 = value * 2
    var value2 = value * 4
    var value3 = value * 8

    return theArray[0] = value1
    return theArray[1] = value2
    return theArray[2] = value3
}

Is there a way to assign a value that comes from a function and assign it to an Array? What useful patterns exist for assigning function values to theArray?

4
  • 1
    you need to return a type that supports multiple values: object, array are two that come to mind. Commented Dec 21, 2017 at 17:59
  • 1
    Your code is completely wrong. Why do you return tuple from the function? Do you know, that it won't return the actual value of the array but Void? Do you know that you have to return tuple in the function? Commented Dec 21, 2017 at 18:23
  • 4
    @jnblanchard The edit you suggested is absolutely inappropriate. If you fix the issue in the question itself then there's no more issue and no need to post answers!!! This is ridiculous. Please never do this again. You probably mean well but this is actually vandalism, it's inacceptable. Commented Dec 21, 2017 at 23:04
  • I apologize, not my intention. This question as it is posed could use some more clarity and readability. I know I share other's pain when it comes to misinterpreting this problem. Commented Dec 22, 2017 at 23:42

5 Answers 5

2

Since it seems you want your result as an array, why not return an array?

func aFunction(value: Int) -> [Int] {
    return [ value * 2, value * 4, value * 8 ]
}

let theArray = aFunction(value: 2)
Sign up to request clarification or add additional context in comments.

1 Comment

Someone care to explain the downvote? The code works. It answers the question.
1

Either your function returns an array or it returns a tuple. If you return a tuple there is nothing stopping you from wrapping it up in an array once you get the value back:

import PlaygroundSupport
import UIKit

var theArray = [1, 2, 3]

func aFunction(value: Int) -> (Int, Int, Int){
  let value1 = value * 2
  let value2 = value * 4
  let value3 = value * 8
  return (value1, value2, value3)
}

var (x,y,z) = aFunction(value: 2)
theArray = [x,y,z]
print(theArray)

output:

[4, 8, 16]

1 Comment

You should mention that for more variables this method becomes quite painful, since there's no method for converting the tuple to an array automatically, so you'd have to write out each element of the tuple manually.
1

You could use the map function:

let value = 3
let theArray = [1, 2, 3].map() { $0 * value }

You can of course embed this un a func, with theArray and value as parameters

This should answer better to the OP:

let value = 2 
var newValue = 1 
var theArray = [1, 2, 3] 
theArray = theArray.map() { (number: Int) -> Int in newValue = newValue * value ; return number * newValue }

6 Comments

What is the value property in this case?
I added the value. But of course, that should be embedded in a func.
This isn't remotely close to what the code in the question appears to be trying to do. With a value of 2, the answer should be [4, 8, 16]. This answer would give [2, 4, 6].
You should map the iterator.
I missed a part of the question, sorry. This should work let value = 2 var newValue = 1 var theArray = [1, 2, 3] theArray = theArray.map() { (number: Int) -> Int in newValue = newValue * value ; return number * newValue }
|
0

Since your code is totally wrong and won't compile, I will provide an answer covering all your aspects:

First of all you need to return Array, not a tuple read the tuple section here

The second thing is you can call map function directly in your code so you don't need to create custom function. Check out this link - it's beautiful

var theArray = [1, 2, 3]

let newArray: [Int] = theArray.enumerated().map { (index, element) in

    switch index {
    case 0:
       return element*2
    case 1:
        return element*4
    case 2:
        return element*8
    default:
        return 0 // You don't really need to do this when you know your Array contains your values. 
    }
}

Other useful method could be with iterating over theArray:

var theArray = [1, 2, 3]

var newArray: [Int] = []
var iterator = 2
for element in theArray {
    iterator = iterator * 2
    newArray.append(element*iterator)
}
print(newArray)

Just wrap these solutions into the function and return the newArray property.

2 Comments

It could be a misunderstanding in the original question but this solution will return an array of [2, 8, 24] when the original requirement appears to multiply a single value by 2, 4 & 8 to get [4, 8, 16]. However everything else about this is correct so still answers the heart of the question
Exactly! That's my answer
-2

A good way might be to create your own update block extension on array.

  extension Array {
    func normalized(update: (Int, Int) -> Int) -> [Int] {
      var updateArray: [Int] = []
      for (index, element) in self.enumerated() {
        guard let temp = element as? Int else { break }
        updateArray.append(update(index, temp))
      }
      return updateArray
    }
  }

Notice that this normalized instance method uses a tuple that includes array position and value.

  var theArray = [1,2,3]

I declared 'our' function as a map that multiplies entries by 2.

  func multiplyMapByTwo(index: Int, value: Int) -> Int {
    let map = [0: 2, 1: 4, 2: 8]
    guard let val = map[index] else { return 0 }
    return 2*val
  }

This is the fun part, we now are able to normalize each of theArray's values using the multiply and map function we just created.

  theArray = theArray.normalized {
    return multiplyMapByTwo(index: $0, value: $1)
  }

  print(theArray)

theArray will have [4, 8, 16].

2 Comments

The expected result is [4, 8, 16]. And the question is asking how to return multiple values from a function and assign the result to an array.
I would have appreciated some clarification rather than a downvote, I think you'll agree, this is better.

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.