Reputation: 446
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?
Upvotes: 0
Views: 5244
Reputation: 884
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 }
Upvotes: 1
Reputation: 1200
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].
Upvotes: -2
Reputation: 2169
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.
Upvotes: 0
Reputation: 16327
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]
Upvotes: 1
Reputation: 318794
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)
Upvotes: 2